簡體   English   中英

C#WP8.1 - 使用Bing地圖時在路徑頂部顯示MapIcon

[英]C# WP8.1 - Displaying a MapIcon on top of a Route when using Bing Maps

我現在正在玩bing地圖。 我已經按照教程創建路線並添加了地圖等,但我發現mapIcon不會顯示它是否在路線上。 我嘗試過使用mapIcon的Z-Index屬性,但這看起來效果不大,因為我認為它只對其他MapElements產生影響。

有沒有其他人知道如何實現這一目標?

我目前用於創建路徑並嘗試在目標上設置MapIcon的代碼是(對於內容MapFunctions的abit只是我為功能所做的靜態類,例如查找路徑,獲取當前位置等):

    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);
    }

如果要在元素頂部添加圖釘,可以使用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));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM