Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the coordinates when user tap on a Map in Xamarin.Forms?

In my Xamarin.Forms (PCL project) I want to get the coordinate (lat & long) when a user taps on map but I can't find any tap event in the Map class. I think using TapGestureRecognizer class's object will not help in this case.

So is there any technique that we can perform in native platform for recognizing tap and getting coordinate of that tapped area/point?

Any blog post or technique will be helpful.

Thanks.

like image 537
Janak Avatar asked Apr 12 '15 18:04

Janak


2 Answers

I implemented simple extended map control to provide tap location for all three platforms. You can have a look here. It's pure sample code, not for production, works fine for me but if you have any problems please tell me.

like image 59
Artur Shamsutdinov Avatar answered Oct 19 '22 08:10

Artur Shamsutdinov


At this point I do not believe any of the nugets out there for dealing with a Xamarin abstraction of maps is going to handle this kind of very specific thing. You will probably have to do native implementations and use a dependency injection pattern to make the appropriate calls.

Interface in PCL

interface IMaps
{
   public GetLatLong();
}

Native Android Implementation

see How to get the Latitude and Longitude on Map in Android?

Native iOS Implementation

see Ios Map: Getting latitude and longitude

Calling Your Implementations in PCL

public point HandleMapOnClick()
{
    return DependencyService.Get<IMaps>().GetLatLong();
}
like image 1
ClintL Avatar answered Oct 19 '22 07:10

ClintL