Question

I've started using maps in a Windows phone application and I have figured out how to get the current coordinates but I'm not sure how to draw it as a pushpin on the map.

This is what I have so far which calls a GetCoordinates method and navigates to the map on a button click.Does anyone know how I could pass the coordinates to the map and draw it as a pushpin?

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

And this is the map class which doesn't do anyting yet, Is there a way I could pass the geocoordinates from the previous class to the map and draw a pushpin? I was thinking of doing this in the OnNavigatedTo method somehow :

public partial class Maps : 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);
    }

}
Was it helpful?

Solution

To add a pin to the map:

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

You can append the latitude and longitude of your position as a query in the URI you pass to NavigationSerivce.Navigate, and extract it in the OnNavigatedTo event handler with e.Uri.Query.

A tip. Task.Run schedules your task to run on the thread-pool. Your task is not CPU-bound and therefore Task.Run will not give you any performance gains.

Edit: Convert a Geoposition to GeoCoordinate with this:

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

A very useful resource is the Nokia WP8 Guide

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top