源代码源自osmdbonusPack的代码,具体实现如下:
package com.etrip.osmdroid; import org.osmdroid.api.IGeoPoint; import org.osmdroid.views.MapView; import org.osmdroid.views.MapView.Projection; import org.osmdroid.views.overlay.Overlay; import android.content.Context; import android.graphics.Canvas; import android.view.MotionEvent; /** * Empty overlay than can be used to detect events on the map, * and to throw them to a MapEventsReceiver. * @see MapEventsReceiver * @author M.Kergall */ public class MapEventsOverlay extends Overlay { private Context context; private MapEventsReceiver mReceiver; private int x; private int y; /** * @param ctx the context * @param receiver the object that will receive/handle the events. * It must implement MapEventsReceiver interface. */ public MapEventsOverlay(Context ctx, int x, int y,MapEventsReceiver receiver) { super(ctx); this.context = ctx; this.x = x; this.y = y; mReceiver = receiver; } @Override protected void draw(Canvas c, MapView osmv, boolean shadow) { } @Override public boolean onSingleTapUp(MotionEvent e, MapView mapView){ Projection proj = mapView.getProjection(); IGeoPoint p = proj.fromPixels(e.getX(), e.getY()); mapView.getController().animateTo(p); return mReceiver.singleTapUpHelper(p); } @Override public boolean onLongPress(MotionEvent e, MapView mapView) { Projection proj = mapView.getProjection(); IGeoPoint p = proj.fromPixels(e.getX(), e.getY()); //throw event to the receiver: return mReceiver.longPressHelper(p); } }
package com.etrip.osmdroid; import org.osmdroid.api.IGeoPoint; /** * Interface for objects that need to handle map events thrown by a MapEventsOverlay. * @see MapEventsOverlay * @author M.Kergall */ public interface MapEventsReceiver { /** * @param p the position where the event occurred. * @return true if the event has been "consumed" and should not be handled by other objects. */ boolean singleTapUpHelper(IGeoPoint p); /** * @param p the position where the event occurred. * @return true if the event has been "consumed" and should not be handled by other objects. */ boolean longPressHelper(IGeoPoint p); }
具体的使用如下:
package com.etrip.osmdroid; import org.osmdroid.api.IGeoPoint; import org.osmdroid.tileprovider.tilesource.TileSourceFactory; import org.osmdroid.util.GeoPoint; import org.osmdroid.views.MapController; import org.osmdroid.views.MapView; import android.app.Activity; import android.os.Bundle; import android.widget.Toast; /** * * @Title: * @Description: 实现TODO * @Copyright:Copyright (c) 2011 * @Company: * @Date:2012-7-31 * @author longgangbai * @version 1.0 */ public class GeoServersmdroidActivity extends Activity { private MapController mapController; private MapView mapView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mapView = (MapView) findViewById(R.id.map); mapView.setTileSource(TileSourceFactory.MAPNIK); mapView.setBuiltInZoomControls(true); mapController = mapView.getController(); mapController.setZoom(15); GeoPoint geopoint=new GeoPoint(39.92605, 116.42616); mapController.setCenter(geopoint); MapEventsOverlay mapEventOverlay=new MapEventsOverlay(this,100,100,new MapEventsReceiver() { @Override public boolean singleTapUpHelper(IGeoPoint p) { Toast.makeText(GeoServersmdroidActivity.this, "test touch event ", Toast.LENGTH_LONG).show(); return true; } @Override public boolean longPressHelper(IGeoPoint p) { return false; } }); mapView.getOverlays().add(mapEventOverlay); mapView.invalidate(); } }