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 method but it isn't even called. Any Idea?
public class MapPoint extends MapActivity implements OnClickListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mappoints);
MapView map = (MapView)findViewById(R.id.mapview);
map.setOnClickListener(this);
map.setBuiltInZoomControls(true);
map.getController().setZoom(18);
map.getController().setCenter(new GeoPoint(39735007, -8827330));
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
public void onClick(View arg0) {
Toast.makeText(this, "text", Toast.LENGTH_SHORT);
}
}
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With