Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get coordinates on tapping map in android

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

}

like image 592
Miguel Ribeiro Avatar asked Nov 14 '10 11:11

Miguel Ribeiro


3 Answers

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.

like image 120
Flo Avatar answered Nov 19 '22 14:11

Flo


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.

like image 29
CommonsWare Avatar answered Nov 19 '22 15:11

CommonsWare


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 image 4
deepwinter Avatar answered Nov 19 '22 13:11

deepwinter