如何从谷歌地图获取lat和long触摸事件?

时间:2011-01-10 12:03:10

标签: android google-maps google-maps-api-3 latitude-longitude

是否可以通过触摸事件从Google地图获取经度和纬度?

例如:如果我在地图上触摸纽约,它应该给我纽约的纬度和经度。

3 个答案:

答案 0 :(得分:15)

您可以直接在GoogleMap对象上设置OnMapClickListener,而不是使用叠加层拦截TouchEvent。

googleMap.setOnMapClickListener(new OnMapClickListener(){
        void onMapClick(LatLng point){
            Toast.makeText(getContext(),
                point.latitude + ", " + point.longitude,
                Toast.LENGTH_SHORT).show();
        }
    });

如何从MapFragmentMapView看到GoogleMap对象 https://developers.google.com/maps/documentation/android/map#add_map_code

答案 1 :(得分:14)

您可以从大多数触摸事件中获取位置,例如

public boolean onTouchEvent(MotionEvent event)
    {
        int X = (int)event.getX();          
        int Y = (int)event.getY();

        GeoPoint geoPoint = mapView.getProjection().fromPixels(X, Y);
    }

答案 2 :(得分:5)

试试这个

 public class Google_maps extends MapActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_google_maps);


        MapView mapview=(MapView)findViewById(R.id.mapView);
        mapview.setBuiltInZoomControls(true);
        MapOverlay mapOverlay = new MapOverlay();
        List<Overlay> listOfOverlays = mapview.getOverlays();
        listOfOverlays.clear();
        listOfOverlays.add(mapOverlay);        

        mapview.invalidate();

    }

    class MapOverlay extends Overlay
    {

        @Override
        public boolean onTouchEvent(MotionEvent event, MapView mapView) 
        {   
            //---when user lifts his finger---
            if (event.getAction() == 1) {                
                GeoPoint p = mapView.getProjection().fromPixels(
                    (int) event.getX(),
                    (int) event.getY());
                    Toast.makeText(getBaseContext(), 
                        p.getLatitudeE6() / 1E6 + "," + 
                        p.getLongitudeE6() /1E6 , 
                        Toast.LENGTH_SHORT).show();


            }                            
            return false;
        }        
    }
    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }}