Get coordinates on tapping map in android
Asked Answered
A

6

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

}

Admit answered 14/11, 2010 at 11:44 Comment(1)
this will help sure..Robbyrobbyn
P
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.

Pruritus answered 14/11, 2010 at 13:38 Comment(0)
M
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.

Merissameristem answered 14/11, 2010 at 13:30 Comment(0)
E
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

Escutcheon answered 12/4, 2014 at 20:14 Comment(1)
link is not valid anymore.Smacking
H
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.

Halvaard answered 4/2, 2011 at 0:29 Comment(0)
M
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;
 }
}
Meehan answered 30/8, 2011 at 15:47 Comment(0)
V
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

Vinegar answered 14/11, 2010 at 14:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.