Android tap on map and get coordinates

醉酒当歌 提交于 2019-12-01 05:54:38

问题


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 coordinates on tapping map in android

It seems like the most relevant post but what I want to do is to be able to tap anywhere on the map not on an marker/overlay item. In fact I want to create a marker in that specific point that the user tapped and get the coordinates of it.

Is that possible?

Thanks

Mike


回答1:


Yes. In fact, the answer I gave in that other question is what you need.




回答2:


Should be trivial with a little bit of math. You can use getLatitudeSpan() and getLongitudeSpan() to get the extents of the visible map, and getMapCenter() to see the center point. Just map that to the coordinates of the touch event.

EDIT: Mark, as always, has an even more elegant solution.




回答3:


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

         }


来源:https://stackoverflow.com/questions/4892365/android-tap-on-map-and-get-coordinates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!