C#WP8.1 - 使用Bing地图时在路径顶部显示MapIcon

时间:2014-07-25 18:24:32

标签: c# windows-phone bing-maps windows-phone-8.1

我现在正在玩bing地图。我已经按照教程创建路线并添加了地图等,但我发现mapIcon不会显示它是否在路线上。我尝试过使用mapIcon的Z-Index属性,但这看起来效果不大,因为我认为它只对其他MapElements产生影响。

还有其他人知道如何实现这一目标吗?

我目前用于创建路线并尝试在目的地上设置MapIcon的代码是(对于内容MapFunctions的升级,我只是为了找到路线,获取当前位置等功能而制作的静态类别):

    private async void DrawRoute()
    {
        // Check if a destination has been set
        if(MapFunctions.destination != null)
        {
            route = await MapFunctions.GetRouteAsync(MapFunctions.destination.Point);

            if (route != null)
            {
                // Use the route to initialize a MapRouteView.
                MapRouteView viewOfRoute = new MapRouteView(route.Route);
                viewOfRoute.RouteColor = Colors.Yellow;
                viewOfRoute.OutlineColor = Colors.Black;


                // Add the new MapRouteView to the Routes collection
                // of the MapControl.
                MyMap.Routes.Add(viewOfRoute);

                // Fit the MapControl to the route.
                await MyMap.TrySetViewBoundsAsync(
                    route.Route.BoundingBox,
                    null,
                    Windows.UI.Xaml.Controls.Maps.MapAnimationKind.None);

                AddDestinationMapElement(MapFunctions.destination.Point);

                // Start timer to update the remaining time of the journey
                UpdateRemainingTime_Tick(this, new Object());
                dispatcherTimer.Start();
            }
            else
            {
                MessageDialog message = new MessageDialog("An error occured while trying to calculate your route. Please try again later.", "Error");
                await message.ShowAsync();
            }
        }
    }

    private void AddDestinationMapElement(Geopoint dest)
    {
        MapIcon MapIcon1 = new MapIcon();
        MapIcon1.Location = new Geopoint(new BasicGeoposition()
        {
            Latitude = dest.Position.Latitude,
            Longitude = dest.Position.Longitude
        });

        MapIcon1.Visible = true;
        MapIcon1.ZIndex = int.MaxValue;
        MapIcon1.Image = RandomAccessStreamReference.CreateFromUri(new Uri("ms-appx:///Images/mapIcon.png"));
        MapIcon1.NormalizedAnchorPoint = new Point(0.5, 1.0);
        MapIcon1.Title = "Destination";
        MyMap.MapElements.Add(MapIcon1);
    }

1 个答案:

答案 0 :(得分:3)

如果要在元素顶部添加图钉,可以使用MapOverlay,它允许您将图钉(带图像或任何XAML元素)添加到地图控件中:

MapOverlay pushpinStart = new MapOverlay();
pushpinStart.PositionOrigin = new Point(0.5, 0.5);
pushpinStart.Content = new Image()
                           {
                               Source =
                                   new BitmapImage(
                                   new Uri("../Assets/Ui/ui-pin-start.png", UriKind.Relative))
                           };
pushpinStart.GeoCoordinate = posCollection[0];

如果你想继续使用MapIcon,那么渲染引擎将根据缩放级别和当前位置以及其他元素碰撞算法的结果来计算最佳显示内容。所以我不确定你在寻找什么。

对于WP8.1,这是等效的代码:

Image iconStart = new Image();
iconStart.Source = new BitmapImage(new Uri("ms-appx:///Assets/Ui/ui-pin-start.png"));
this.Map.Children.Add(iconStart);
MapControl.SetLocation(iconStart, new Geopoint(pos[0]));
MapControl.SetNormalizedAnchorPoint(iconStart, new Point(0.5, 0.5));