问题
I'm coding an application using osmdroid to show a map and i would like to add a marker to it when user taps over map, actually i have been able to do this usin motion event but this adds a marker even when user is zooming in or out the map i don't want this. This is the code i have to add the marker:
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
int actionType = ev.getAction();
switch (actionType) {
case MotionEvent.ACTION_DOWN:
Projection proj = myOpenMapView.getProjection();
GeoPoint loc = (GeoPoint) proj.fromPixels((int)ev.getX(), (int)ev.getY());
String longitude = Double.toString(((double)loc.getLongitudeE6())/1000000);
String latitude = Double.toString(((double)loc.getLatitudeE6())/1000000);
List<OverlayItem> anotherOverlayItemArray = new ArrayList<OverlayItem>();
ExtendedOverlayItem mapItem = new ExtendedOverlayItem("", "", new GeoPoint((((double)loc.getLatitudeE6())/1000000), (((double)loc.getLongitudeE6())/1000000)), this);
mapItem.setMarker(this.getResources().getDrawable(R.drawable.marker));
anotherOverlayItemArray.add(mapItem);
ItemizedIconOverlay<OverlayItem> anotherItemizedIconOverlay
= new ItemizedIconOverlay<OverlayItem>(
this, anotherOverlayItemArray, null);
myOpenMapView.getOverlays().add(anotherItemizedIconOverlay);
Toast toast = Toast.makeText(getApplicationContext(), "Longitude: "+ longitude +" Latitude: "+ latitude , Toast.LENGTH_LONG);
toast.show();
}
return super.dispatchTouchEvent(ev);
}
This is how i finally solved it:
Overlay touchOverlay = new Overlay(this){
ItemizedIconOverlay<OverlayItem> anotherItemizedIconOverlay = null;
@Override
protected void draw(Canvas arg0, MapView arg1, boolean arg2) {
}
@Override
public boolean onSingleTapConfirmed(final MotionEvent e, final MapView mapView) {
Projection proj = mapView.getProjection();
GeoPoint loc = (GeoPoint) proj.fromPixels((int)e.getX(), (int)e.getY());
longitude = Double.toString(((double)loc.getLongitudeE6())/1000000);
latitude = Double.toString(((double)loc.getLatitudeE6())/1000000);
ArrayList<OverlayItem> overlayArray = new ArrayList<OverlayItem>();
OverlayItem mapItem = new OverlayItem("", "", new GeoPoint((((double)loc.getLatitudeE6())/1000000), (((double)loc.getLongitudeE6())/1000000)));
mapItem.setMarker(marker);
overlayArray.add(mapItem);
if(anotherItemizedIconOverlay==null){
anotherItemizedIconOverlay = new ItemizedIconOverlay<OverlayItem>(getApplicationContext(), overlayArray,null);
myOpenMapView.getOverlays().add(anotherItemizedIconOverlay);
myOpenMapView.invalidate();
}else{
myOpenMapView.getOverlays().remove(anotherItemizedIconOverlay);
myOpenMapView.invalidate();
anotherItemizedIconOverlay = new ItemizedIconOverlay<OverlayItem>(getApplicationContext(), overlayArray,null);
myOpenMapView.getOverlays().add(anotherItemizedIconOverlay);
}
dlgThread();
return true;
}
};
myOpenMapView.getOverlays().add(touchOverlay);
回答1:
You should create an Overlay and override the onSingleTapConfirmed() method to get single-taps:
@Override
public boolean onSingleTapConfirmed(final MotionEvent event, final MapView mapView) {
// Handle single-tap here, then return true.
return true;
}
来源:https://stackoverflow.com/questions/18613992/add-marker-to-map-on-tap