问题
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);
}
}
回答1:
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.
回答2:
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.
回答3:
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
回答4:
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.
回答5:
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;
}
}
回答6:
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
来源:https://stackoverflow.com/questions/4177305/get-coordinates-on-tapping-map-in-android