Calculate and draw a route on Bing Maps control

in my application for WP7 (mango) I need to move the user from one point to another. I know there is a Map control that allows you to draw staff, but how do you ask him to draw a path for you? (based on the specified destination and the current location of the user - but it continues to change, since you are updating the route if it goes out of the way?)

+4
source share
1 answer

To update the map with the current location of users, use GeoCoordinateWatcher and update the position of the Push-in button to change data as it changes. Do not forget to set the minimum distance to something low, for example, 5 meters.

For this XAML template, a button similar to the one on the bing cards can be created:

 <maps:Pushpin Background="{StaticResource PushpinLocationBrush}" Location="{Binding MyLocation}"> <maps:Pushpin.Template> <ControlTemplate> <Grid> <Rectangle Width="15" Height="15" Margin="0" Fill="Black"> <Rectangle.Projection> <PlaneProjection CenterOfRotationX="0" LocalOffsetX="-2" LocalOffsetY="5" RotationZ="45" /> </Rectangle.Projection> </Rectangle> <Ellipse Width="7" Height="7" Margin="0" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="Orange" RenderTransformOrigin="0.339,0.232" StrokeThickness="0" /> </Grid> </ControlTemplate> </maps:Pushpin.Template> </maps:Pushpin> 

Obtaining a GeoCoordinate address can be done using Bing Maps. You can learn more about Bing Services here: http://msdn.microsoft.com/en-us/library/cc980922.aspx - the one you need is GeoCodeService

The drawing of the path is rather complicated, especially if you want it to follow the roads. To do this, you need the Bing Maps Route Service.

Add the service in Visual Studio with RouteServiceReference as a name, and then you can use the following code to get the fragments of the path and add them to your map. The XAML below reflects the controls to which I add fragments:

 List<GeoCoordinate> locations = new List<GeoCoordinate>(); RouteServiceClient routeService = new RouteServiceClient("BasicHttpBinding_IRouteService"); routeService.CalculateRouteCompleted += (sender, e) => { var points = e.Result.Result.RoutePath.Points; var coordinates = points.Select(x => new GeoCoordinate(x.Latitude, x.Longitude)); var routeColor = Colors.Blue; var routeBrush = new SolidColorBrush(routeColor); var routeLine = new MapPolyline() { Locations = new LocationCollection(), Stroke = routeBrush, Opacity = 0.65, StrokeThickness = 5.0, }; foreach (var location in points) { routeLine.Locations.Add(new GeoCoordinate(location.Latitude, location.Longitude)); } RouteLayer.Children.Add(routeLine); }; RouteBingMap.SetView(LocationRect.CreateLocationRect(locations)); routeService.CalculateRouteAsync(new RouteRequest() { Credentials = new Credentials() { ApplicationId = "YOURBINGMAPSKEYHERE" }, Options = new RouteOptions() { RoutePathType = RoutePathType.Points }, Waypoints = new ObservableCollection<Waypoint>( locations.Select(x => new Waypoint() { Location = x.Location })) }); 

XAML related:

 <maps:Map x:Name="RouteBingMap" AnimationLevel="None" CopyrightVisibility="Collapsed" CredentialsProvider="YOURBINGMAPSKEYHERE" LogoVisibility="Collapsed" ZoomBarVisibility="Collapsed" ZoomLevel="12"> <maps:MapLayer x:Name="RouteLayer" /> </maps:Map> 
+15
source

All Articles