Get coordinates on tapping map in android

前端 未结 6 1746
北海茫月
北海茫月 2020-12-01 12:49

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 metho

相关标签:
6条回答
  • 2020-12-01 13:19

    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;
     }
    }
    
    0 讨论(0)
  • 2020-12-01 13:24

    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.

    0 讨论(0)
  • 2020-12-01 13:25

    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

    0 讨论(0)
  • 2020-12-01 13:25

    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

    0 讨论(0)
  • 2020-12-01 13:37

    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.

    0 讨论(0)
  • 2020-12-01 13:37

    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.

    0 讨论(0)
提交回复
热议问题