如何在谷歌地图中更改MyLocationOverlay的默认蓝色动画标记?
发布于 2010-11-14 08:13:39
步骤1:创建MyLocationOverlay的子类。
步骤2:覆盖drawMyLocation()并随心所欲地绘制标记。请记住,此方法不仅绘制标记,而且“如果用户的位置移动到屏幕边缘附近,并且构造函数中给了我们一个MapController,我们将滚动以使新读数重新居中”。
发布于 2011-03-06 22:46:07
Thx @CommonsWare,你把我引向了正确的方向。在这件事上花了不少心思。http://code.google.com/android/add-ons/google-apis/reference/com/google/android/maps/MyLocationOverlay.html上的Javadoc是错误的(或过时的),它会扰乱你的大脑,因为它说:
drawMyLocation
此外,如果用户的位置移动到屏幕边缘附近,并且构造函数中给了我们一个MapController,我们将滚动到新读数的中心位置。
这是完全错误的。首先,类中没有这样的构造函数。其次,只有当当前位置在视界内,或者屏幕被移动时,才会调用drawMyLocation。因此,如果您收到一个新位置,但此时看不到标记,它将不会被重新绘制。一般来说,这是一件好事,但如果您想在方法中实现mc.animateTo以保持位置居中,则这是一件坏事。
第三,您不需要传递MapController来启用currentLocation,因为您已经拥有了mapView并从中获取了控制器。
因此,我最终编写了自己的类,一旦位置到达mapView的边界或不在屏幕上,它就会一直以当前位置为中心:
public class CurrentLocationOverlay extends MyLocationOverlay {
// TODO: use dynamic calculation?
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.position);
}
@Override
protected void drawMyLocation(Canvas canvas, MapView mapView, Location lastFix, GeoPoint myLocation, long when) {
// TODO: find a better way to get height/width once the mapView is layed out correctly
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());
}
}
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;
}
}https://stackoverflow.com/questions/4175432
复制相似问题