1

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

}
Brian Var
  • 6,029
  • 25
  • 114
  • 212

1 Answers1

2

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

JonPall
  • 814
  • 4
  • 9
  • that helps a lot but how can I pass the myGeoPosition data from my other class to the map class? Also the line ` GeoCoordinate = location, ` location could be substituted with `myGeoPosition` ? – Brian Var Mar 10 '14 at 21:46
  • 1
    @BrianJ First of all, you need to return it from your GetCoordinates() method. Yes, you can convert `myGeoPosition` to a GeoCoordinate. See my edit. Read here on how to pass a parameter to the new page: http://stackoverflow.com/questions/12444816/how-to-pass-values-parameters-between-xaml-pages – JonPall Mar 10 '14 at 21:54
  • I'll try that out later,thanks for the help, I'll let you know if it works out. – Brian Var Mar 10 '14 at 23:12