android: MapView does not recognize clicks

前端 未结 4 768
臣服心动
臣服心动 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:34

    I used something similar to kgiannakakis' answer, with an inner MapOverlay class, but I overloaded the OnTap() method to provide an Exit_Dialog to close down the activity. This implementation still allows for zoom and drag control on the MapView without affecting the Overlay control. But you must use mMapView.getContext() in the AlertDialog.Builder construction. See code:

    public class TripDataMapActivity extends MapActivity { 
    
        ....
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.mapview_activity);
    
            // Set result CANCELED in case the user backs out
            setResult(Activity.RESULT_CANCELED);
    
            mMapView = (MapView)findViewById(R.id.mapview);
            mMapView.setBuiltInZoomControls(true);
    
            mMapCntrl = mMapView.getController();
            mMapCntrl.setZoom(14);  // World View is Zoom 1            
    
            // Set-up Drawable Overlays
            mMapOverlays = mMapView.getOverlays();
            mMapOverlays.clear();
            mDrawable = this.getResources().getDrawable(R.drawable.direction_arrow);
            mItemizedOverlay = new MyItemizedOverlays(mDrawable);
            updateMapView();
        }
        .....
    
        protected void updateMapView() { 
            // Convert Location into GeoPoint.
            int lat = (int)(locatn.getLatitude() * 1E6);  
            int lng = (int)(locatn.getLongitude() * 1E6); 
            GeoPoint point = new GeoPoint(lat, lng);
            OverlayItem overlayitem = new OverlayItem(point, "someTitle!", "someSnippet!");
            mMapCntrl.setCenter(point); 
    
            mItemizedOverlay.addOverlay(overlayitem);
            mMapOverlays.add(mItemizedOverlay);
            mMapView.invalidate();
        }
        ....
        // Inner Class Implementation 
        class MyItemizedOverlays extends ItemizedOverlay {
    
        private ArrayList mOverlays = new ArrayList(); 
    
        public MyItemizedOverlays(Drawable defaultMarker) {
            super(boundCenterBottom(defaultMarker));
        }
    
        @Override
        protected OverlayItem createItem(int ith) { return mOverlays.get(ith); }
    
        @Override
        public int size() { return mOverlays.size(); }
    
        public void addOverlay(OverlayItem overlay) {
        mOverlays.add(overlay);
        populate();  // will call createItem(int) above 
        }
    
        @Override
        protected boolean onTap(int index) {
    
        // EXIT Dialog
        AlertDialog.Builder exitDialog = 
                new AlertDialog.Builder(mMapView.getContext());
            exitDialog.setMessage("Are you sure you want to Exit?")
                              .setCancelable(false)
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
    
                    public void onClick(DialogInterface dialog, int id) {
    
                Intent intent = new Intent();
                //intent.putExtra(EXTRA_DEVICE_ADDRESS, address);
                // Set result and finish this Activity
                setResult(Activity.RESULT_OK, intent);
    
                TripDataMapActivity.this.finish();
                }
            }).setNegativeButton("No", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                   }
            });
            exitDialog.show();
    
        return true;
        }
        // End of Inner Class
    }
    
    }     
    

提交回复
热议问题