windows-phone-7 – 在Bing Maps控件上计算和绘制路径

前端之家收集整理的这篇文章主要介绍了windows-phone-7 – 在Bing Maps控件上计算和绘制路径前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在我的WP7(芒果)应用程序中,我需要将用户从一个点导航到另一个点.我知道有一个Map控件可以让你在上面绘制工作人员,但是你如何要求它为你绘制路径? (根据指定的目的地和用户的当前位置 – 但不断变化,如果他这样做,你如何更新路线?)
要使用用户当前位置更新地图,请使用GeoCoordinateWatcher并在数据库更改时更新数据绑定Pushpin的位置.请记住将最小距离设置为低,例如5米.

可以使用此XAML模板创建类似于bing贴图上的图钉:

<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可以获得地址的GeoCoordinate.您可以在此处阅读有关Bing服务的更多信息:http://msdn.microsoft.com/en-us/library/cc980922.aspx – 您需要的是GeoCodeService

绘制路径相当复杂,特别是如果您希望它遵循道路.为此,您需要Bing Maps Route Service.

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

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>

猜你在找的Windows相关文章