16

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);
}

}

Miguel Ribeiro
  • 8,057
  • 20
  • 51
  • 74

6 Answers6

17

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.

Flo
  • 27,355
  • 15
  • 87
  • 125
7

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
4

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

deepwinter
  • 4,568
  • 2
  • 31
  • 37
2

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.

Community
  • 1
  • 1
Ollie C
  • 28,313
  • 34
  • 134
  • 217
2

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;
 }
}
andoni90
  • 1,066
  • 2
  • 12
  • 30
0

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

Rohith Nandakumar
  • 11,367
  • 11
  • 50
  • 60