在许多用地图应用中,在地图第一次加载的时候,可能将用户当前的位置定位的地图的中心,有时候可能有一个定位的功能,那么当点击点位的时候,需要地图的以当前用户所在位置为中心那么就需要重写MyLocationOverlay类实现相关的功能。
针对MyLocationOverlay的重写如下:
package com.etrip.osmdroid; import org.osmdroid.util.GeoPoint; import org.osmdroid.views.MapController; import org.osmdroid.views.MapView; import org.osmdroid.views.overlay.MyLocationOverlay; import com.etrip.osmdroid.R; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Point; import android.location.Location; /** * 重写的目的是在于 * 1.让当前的位置位于地图的中心位置 * * * 原文: * http://stackoverflow.com/questions/4175432/mylocationoverlay-change-the-marker * * 如果想改变当前位置的图标和标记颜色,可以采用以下两种方式实现: * Step #1: Create a subclass of MyLocationOverlay. * Step #2: Override drawMyLocation() * * * * */ public class CurrentLocationOverlay extends MyLocationOverlay { private final static int PADDING_ACTIVE_ZOOM = 50; private MapController mc; private Bitmap marker; private Point currentPoint = new Point(); private boolean centerOnCurrentLocation = true; private int height; private int width; /** * By default this CurrentLocationOverlay will center on the current location, * if the currentLocation is near the * edge, or off the screen. To dynamically enable/disable this, * use {@link #setCenterOnCurrentLocation(boolean)}. * @param context * @param mapView */ public CurrentLocationOverlay(Context context, MapView mapView) { super(context, mapView); this.mc = mapView.getController(); this.marker = BitmapFactory.decodeResource(context.getResources(), R.drawable.marker_default); } protected void drawMyLocation(Canvas canvas, MapView mapView, Location lastFix, GeoPoint myLocation, long when) { if (this.height == 0) { this.height = mapView.getHeight(); this.width = mapView.getWidth(); } mapView.getProjection().toPixels(myLocation, currentPoint); canvas.drawBitmap(marker, currentPoint.x, currentPoint.y - 40, null); } @Override public synchronized void onLocationChanged(Location location) { super.onLocationChanged(location); // only move to new position if enabled and we are in an border-area if (mc != null && centerOnCurrentLocation && inZoomActiveArea(currentPoint)) { mc.animateTo(getMyLocation()); setCenterOnCurrentLocation(true); } } private boolean inZoomActiveArea(Point currentPoint) { if ((currentPoint.x > PADDING_ACTIVE_ZOOM && currentPoint.x < width - PADDING_ACTIVE_ZOOM) && (currentPoint.y > PADDING_ACTIVE_ZOOM && currentPoint.y < height - PADDING_ACTIVE_ZOOM)) { return false; } return true; } public void setCenterOnCurrentLocation(boolean centerOnCurrentLocation) { this.centerOnCurrentLocation = centerOnCurrentLocation; } }
测试代码如下:
package com.etrip.osmdroid; 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; /** * * * * @author longgangbai */ public class MainActivity extends Activity { private MapController mapController; private MapView mapView; /** Called when the activity is first created. */ @Override public void onCreate(final 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(8); CurrentLocationOverlay locationOverlay=new CurrentLocationOverlay(this,mapView); locationOverlay.enableCompass(); locationOverlay.enableFollowLocation(); locationOverlay.enableMyLocation(); //GeoPoint geopoint=new GeoPoint(39.92605, 116.42616); // mapController.setCenter(locationOverlay.getMyLocation()); mapView.getOverlays().add(locationOverlay); } }