【问题标题】:C# WP8.1 - Displaying a MapIcon on top of a Route when using Bing MapsC# WP8.1 - 使用 Bing 地图时在路线顶部显示 MapIcon
【发布时间】:2014-07-25 18:24:32
【问题描述】:

我现在只是在玩 bing 地图。我已按照教程创建路线并添加地图图标等,但是我发现如果地图图标在路线上,则不会显示。我尝试过使用 mapIcon 的 Z-Index 属性,但这似乎没有什么影响,因为我认为它只对其他 MapElements 有影响。

还有其他人知道实现这一点的方法吗?

我当前用于创建路线并尝试在目的地上设置 MapIcon 的代码是(对于内容的 abit,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);
    }

【问题讨论】:

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


    【解决方案1】:

    如果您想在元素顶部添加图钉,可以使用 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));
    

    【讨论】:

    • 感谢您的回答。我以前从未见过 MapOverlay 类,在这种情况下,听起来它们可能比 MapIcons 更好。
    • 似乎 MapOverlay 类仅在 WP8 中可用,而不是 WP8.1,因为我在 WP8.1 的任何命名空间中都找不到它
    • 我添加了 WP8.1 等效代码来帮助您,如果您需要更多帮助,请告诉我。
    • 要指出另一个区别,MapIcons 由地图控件本身呈现,而 MapOverlay 实际上是 XAML 中的叠加层。所以当有很多叠加层时,它们可能会变慢。
    • 这似乎不起作用。我没有收到任何错误,但我在地图上看不到图钉
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多