Android tap on map and get coordinates

前端 未结 3 1606
北海茫月
北海茫月 2021-01-13 13:25

Hi guys I am trying to make an app that once the user taps on a map it gets the coordinates of that specific point.

Among others i have also read that: Get coordina

3条回答
  •  感情败类
    2021-01-13 14:16

    You will need to override onTouchEvent().

    i hope following code will help u... showing using onTouchEvent() to while user lifts his finger from map...

    Given the screen coordinates of the touch, you can use a Projection (from getProjection() on MapView) to convert that to latitude and longitude...

    @Override
            public boolean onTouchEvent(MotionEvent event, MapView mapView) 
            {   
                GeoPoint point_touch;
                MapController map_controller=mapView.getController();
                //---when user lifts his finger---
                if (event.getAction() == event.ACTION_UP) {                
                    point_touch = mapView.getProjection().fromPixels((int) event.getX(),(int) event.getY());                    
                    Log.i("Cordinates","Lattitude="+point_touch.getLatitudeE6() / 1E6 
                            +" Longitude="+point_touch.getLongitudeE6() /1E6  );
                    map_controller.animateTo(point_touch);
                    return true;
                }    
                else
                {
                    return false;
                }
    
             }
    

提交回复
热议问题