I\'m trying to make something like this: I have a mapactivity and when the user taps the map it shows the coordinates of that location. I already overrided the onclick metho
Try this:
private class OverlayMapa extends Overlay {
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow)
{
...
}
@Override
public boolean onTap(GeoPoint point, MapView mapView)
{
Context contexto = mapView.getContext();
String msg = "Lat: " + point.getLatitudeE6()/1E6 + " - " +
"Lon: " + point.getLongitudeE6()/1E6;
Toast toast = Toast.makeText(contexto, msg, Toast.LENGTH_SHORT);
toast.show();
return true;
}
}
Try the following.
Write a class which derives from the Overlay class and override the onTap() method. Then you can add your overlay to the your MapView. A GeoPoint object, which represents the position of you tap, is passed to the onTap() method when you tab somewhere on the map.
The modern answer, using Android Maps v2, is to use OnMapClickListener, which gives you the LatLng of a tap on the map.
https://developer.android.com/reference/com/google/android/gms/maps/GoogleMap.OnMapClickListener.html
Like @CommonsWare said, you need to use onTouch()
function and not onClick()
. Here is an example, this is exactly what you need : http://mobiforge.com/developing/story/using-google-maps-android
onClick()
is not used here. You will need to override onTouchEvent()
. Here is a sample project showing using onTouchEvent()
to drag and drop ItemizedOverlay
map pins.
Given the screen coordinates of the touch, you can use a Projection
(from getProjection()
on MapView
) to convert that to latitude and longitude.
I put some details of my implementation in this question: How do I respond to a tap on an Android MapView, but ignore pinch-zoom?
The problem I found with using onTap() is that it also gets fired when you pinch-zoom, which was not what my app needed. So if you need pinch-zoom, my code should do what you need.