How to add a dynamic PushPin to map in WP8.1

183 Views Asked by At

I'm currently trying to develop an simple application that allows user to add new pushpins. I'm currently using following code to add new pushpins,

        Pushpin pushpin1 = new Pushpin();

        pushpin1.GeoCoordinate = MyGeoPosition;
        pushpin1.Content = "My car";
        MapOverlay overlay1 = new MapOverlay();
        overlay1.Content = pushpin1;
        overlay1.GeoCoordinate = MyGeoPosition;
        layer1.Add(overlay1);

        myMap.Layers.Add(layer1);

But, to use this code, I need the latitude and longitude of the location that user has selected. So how can I get the latitude and longitude of the location that user has selected. (Simply geo-coordinate) I know I need to write a event handler, But I don't know the way that it has to be implemented... Thank you...

2

There are 2 best solutions below

0
On BEST ANSWER

Try This

MapA = new Map();
MapA.Tap += MapA_Tap;

void MapA_Tap(object sender, System.Windows.Input.GestureEventArgs e)
    {
         GeoCoordinate location = MapA.ConvertViewportPointToGeoCoordinate(e.GetPosition(MapA));

 //display pushpin by using location.latitude and location.longitude
}
1
On

There are multiple map events like

CenterChaged, MapTapped

you can handle those events to add your push pin. here is MapTapped example

 async private void map_MapTapped(MapControl sender, MapInputEventArgs args)
    {
       var MyGeoPosition = args.Location;
      //....
    }