android: MapView does not recognize clicks

前端 未结 4 762
臣服心动
臣服心动 2020-12-17 06:09

I simply want a mapview to recognize a click, in order to call another activity.

Until now, i tried the regular \"onClick\",that always worked for me in regular View

4条回答
  •  悲哀的现实
    2020-12-17 06:36

    Add an Overlay in your map view and handle the OnTouchEvent. Try something like this:

    public class MyMapActivity extends MapActivity {
    
    class MapOverlay extends com.google.android.maps.Overlay
    {       
        @Override
        public boolean onTouchEvent(MotionEvent e, MapView mapView) 
        {   
            if (e.getAction() == MotionEvent.ACTION_UP) {                
                GeoPoint p = mapView.getProjection().fromPixels(
                    (int) e.getX(),
                    (int) e.getY());
                MyMapActivity this.startActivityForResult(intent, requestCode);
            }                            
            return false;
        }        
    }
    
          // MyMapActivity methods
          @Override
          public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            mapView = (MapView) findViewById(R.id.mapview);
    
            MapOverlay mapOverlay = new MapOverlay();
            List listOfOverlays = mapView.getOverlays();
            listOfOverlays.clear();
            listOfOverlays.add(mapOverlay);
      }
    }
    

提交回复
热议问题