开发者

how to get lat and long on touch event from google map?

开发者 https://www.devze.com 2023-02-03 05:04 出处:网络
Is it possible to get latitude and longitude from Google map on touch event? For example: if i touch the New York on map, it should give me the l开发者_如何学运维atitude and longitude of New York.Ins

Is it possible to get latitude and longitude from Google map on touch event?

For example: if i touch the New York on map, it should give me the l开发者_如何学运维atitude and longitude of New York.


Instead of intercepting the TouchEvent with an overlay you can set an OnMapClickListener directly on the GoogleMap object.

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

How to get the GoogleMap Object from a MapFragment or a MapView see https://developers.google.com/maps/documentation/android/map#add_map_code


You can get the location from most of the touch events, e.g.

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

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


Try this

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

精彩评论

暂无评论...
验证码 换一张
取 消