在Bing Maps控件上计算并绘制路线 [英] Calculate and draw route on Bing Maps control

查看:71
本文介绍了在Bing Maps控件上计算并绘制路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的WP7(mango)应用程序中,我需要将用户从一个点导航到另一个点.我知道有一个Map控件可以让您在上面绘制人员,但是您如何要求它为您绘制路径呢? (基于指定的目的地和用户的当前位置-但是该位置会不断变化,因此如果他离开了该如何更新路线?)

in my app for WP7(mango) I need to navigate the user from one point to another. I know there is the Map control that lets you draw staff on it, but how do you ask it to draw the path for you? (based on the specified destination & user's current location - but that keeps changing so how do you update the route if he goes of the way?)

推荐答案

要使用用户当前位置更新地图,请使用GeoCoordinateWatcher并在数据绑定图钉更改时更新其位置.请记住,将最小距离设置为较低的距离,例如5米.

To update the map with the users current location, use the GeoCoordinateWatcher and update the position of a databound Pushpin as it changes. Remember to set the minimum distance to something low, like 5 meters.

可以使用此XAML模板创建图钉地图上的图钉:

A pushpin like the one on bing maps, can be created with this XAML template:

<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>

可以使用Bing Maps获取地址的地理坐标.您可以在此处阅读有关Bing Services的更多信息: http://msdn.microsoft.com/en-us/library/cc980922.aspx -您需要的是 GeoCodeService

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

绘制路径相当复杂,特别是如果您要沿着道路行驶.为此,您需要Bing Maps Route Service.

Drawing a path is rather complicated, specially if you want it to follow the roads. For this, you need the Bing Maps Route Service.

使用RouteServiceReference作为名称将服务添加到Visual Studio,然后您可以利用以下代码获取路径片段,并将其添加到地图中.下面的XAML反映了我向其中添加片段的控件:

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

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:

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

这篇关于在Bing Maps控件上计算并绘制路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆