如何绘制&将地图图钉从当前位置保存到地图? Windows手机

时间:2014-03-10 21:18:23

标签: c# windows-phone-8 geocode here-api pushpin

我已经开始在Windows手机应用程序中使用地图了,我已经弄清楚如何获取当前坐标,但我不知道如何在地图上绘制它作为图钉。

这是我到目前为止调用GetCoordinates方法并通过按钮点击导航到地图。有人知道如何将坐标传递到地图并将其绘制为图钉吗?

private async Task GetCoordinates(string name = "My Car")
        {
            await Task.Run(async () =>
            {
                // Get the phone's current location.
                Geolocator MyGeolocator = new Geolocator();
                MyGeolocator.DesiredAccuracyInMeters = 5;
                Geoposition MyGeoPosition = null;
                try
                {
                    MyGeoPosition = await MyGeolocator.GetGeopositionAsync(TimeSpan.FromMinutes(1), TimeSpan.FromSeconds(10));

                }
                catch (UnauthorizedAccessException)
                {
                    MessageBox.Show("Location is disabled in phone settings or capabilities are not checked.");
                }
                catch (Exception ex)
                {
                    // Something else happened while acquiring the location.
                    MessageBox.Show(ex.Message);
                }
            });
        }


        //sets location of parking space using the GetCoordinates method
        //opens map 
        private async void setLocationBtn_Click(object sender, RoutedEventArgs e)
        { 
            await this.GetCoordinates();
            NavigationService.Navigate(new Uri("/Maps.xaml", UriKind.Relative));
        }

这是地图类,它还没有做任何事情,有没有办法可以将地理坐标从前一个类传递到地图并绘制图钉?我想在某种程度上在OnNavigatedTo方法中这样做:

公共部分类地图:PhoneApplicationPage     {         Geolocator geolocator = null;         bool tracking = false;         ProgressIndicator pi;         MapLayer PushpinMapLayer;

    public Maps()
    {
        InitializeComponent();
        pi = new ProgressIndicator();
        pi.IsIndeterminate = true;
        pi.IsVisible = false;
    }


    protected override void OnNavigatedTo(NavigationEventArgs e)
    {

        base.OnNavigatedTo(e);
    }

}

1 个答案:

答案 0 :(得分:2)

要向地图添加图钉:

        var overlay = new MapOverlay
        {
            PositionOrigin = new Point(0.5, 0.5),
            GeoCoordinate = location, // takes a GeoCoordinate instance. convert Geoposition to GeoCoordinate
            Content = new TextBlock{Text = "hello"}; // you can use any UIElement as a pin
        };

        var ml = new MapLayer { overlay }; 
        map.Layers.Add(ml);

您可以在传递给NavigationSerivce.Navigate的URI中将位置的纬度和经度作为查询附加,并使用OnNavigatedToe.Uri.Query事件处理程序中提取。

小费。 Task.Run计划您的任务在线程池上运行。您的任务不受CPU限制,因此Task.Run不会给您带来任何性能提升。

修改 将Geoposition转换为GeoCoordinate:

var location = new GeoCoordinate(geoposition.Coordinate.Latitude, geoposition.Coordinate.Longitude); 

一个非常有用的资源是Nokia WP8 Guide