我使用了类似于 kgiannakakis 的答案,带有一个内部 MapOverlay 类,但我重载了 OnTap() 方法以提供一个 Exit_Dialog 来关闭活动。此实现仍然允许在 MapView 上进行缩放和拖动控制,而不会影响 Overlay 控件。但是您必须在 AlertDialog.Builder 构造中使用 mMapView.getContext() 。见代码:
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<OverlayItem> {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
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
}
}