Display Dialog Box when i click on the maps place

2019-06-13 16:05发布

问题:

When I click the certain place in the map in android it shows the alert Dialog ? You want to save the location into particular list for example My place or Favorite so that I can move that place to particular list in.

Thanks in Advance......

回答1:

public class MapViewer extends MapActivity {

MapView mapView;

MapController mapController;

GeoPoint mgeoPoint;

Drawable marker;

MyLocationOverlay mLocationOverlay;

MotionEvent e;


@Override
protected void onCreate(Bundle icicle) {

    // TODO Auto-generated method stub
    super.onCreate(icicle);
    setContentView(R.layout.main);

    mapView = (MapView) findViewById(R.id.mapView);

    mapController = mapView.getController();

    mapView.setBuiltInZoomControls(true);

    mapView.setStreetView(true);


    marker = getResources().getDrawable(R.drawable.pushpin);

    marker.setBounds(0, 0, marker.getIntrinsicWidth(), marker
            .getIntrinsicHeight());

    mapView.getOverlays().add(new MapOverlay(marker));


    mLocationOverlay = new MyLocationOverlay(this, mapView);
    mapView.getOverlays().add(mLocationOverlay);

    setViewLocation();
}


@Override
protected Dialog onCreateDialog(int id) {

    // TODO Auto-generated method stub

    switch (id) {
        case 0:
            return new AlertDialog.Builder(this).setTitle("Hello").setIcon(
                    R.drawable.ic_launcher).setPositiveButton("Yes",
                    new OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog , int which) {

                            // TODO Auto-generated method stub

                        }
                    }).setCancelable(true).setNegativeButton("Cancel",
                    new OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog , int which) {

                            // TODO Auto-generated method stub

                        }
                    })

            .setOnCancelListener(new OnCancelListener() {

                @Override
                public void onCancel(DialogInterface dialog) {

                    // TODO Auto-generated method stub
                    Toast.makeText(getApplicationContext(), "Dismiss",
                            Toast.LENGTH_SHORT).show();
                }

            }).create();


        default:
            break;
    }
    return null;
        }


private void setViewLocation() {



    String[] coordinates = { "22.716221", "75.896816" };

    double lat = Double.parseDouble(coordinates[0]);
    double lng = Double.parseDouble(coordinates[1]);
    mgeoPoint = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));

    mapController.animateTo(mgeoPoint);
    mapController.setZoom(15);

    mapView.invalidate();

}


@Override
protected boolean isRouteDisplayed() {

    // TODO Auto-generated method stub
    return false;
}


Point scrPoint;


private GeoPoint getPoint(double lat , double lon) {

    return (new GeoPoint((int) (lat * 1E6), (int) (lon * 1E6)));
}


class MapOverlay extends
        com.google.android.maps.ItemizedOverlay<OverlayItem> {

    List<OverlayItem> ListofGeopoints = new ArrayList<OverlayItem>();


    public MapOverlay(Drawable defaultMarker ) {

        super(defaultMarker);

        ListofGeopoints.add(new OverlayItem(getPoint(22.716221, 75.896816),
                "IN", "India"));

        populate();

    }


    @Override
    protected boolean onTap(int index) {

    switch (index) {
            case 0:
                Toast.makeText(getApplicationContext(), "GeoLocation : 0",
                        Toast.LENGTH_LONG).show();
                showDialog(0);
            break;
        }

        return true;
    }


    String add = "";

    List<Address> add_List = new ArrayList<Address>();


    private void getAddress() {

        add_List = ReverseGeocode
                .getFromLocation(35.594227, -105.223618, 2);

    }


    @Override
    protected OverlayItem createItem(int i) {
        return (ListofGeopoints.get(i));
    }


    @Override
    public int size() {
        return ListofGeopoints.size();
    }
}
}


回答2:

Have you tried to override the onTap method in your customOverlays ?

public class CustomOverlays extends ItemizedOverlay<OverlayItem>
{
    @Override
    public boolean onTap(GeoPoint arg0, MapView arg1) {
        // TODO Auto-generated method stub
        return super.onTap(arg0, arg1);
    }
}