DAr*_*rkO 4 java maps android google-maps marker
如何在谷歌地图中更改MyLocationOverlay的默认蓝色动画标记?
感谢@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;
}
}
Run Code Online (Sandbox Code Playgroud)
步骤#1:创建一个子类MyLocationOverlay
.
步骤#2:根据需要覆盖drawMyLocation()
并绘制标记.请记住,此方法不仅会绘制标记,而且"如果用户的位置移动到屏幕边缘附近,并且我们已经MapController
在构造函数中给出了一个,我们将滚动以重新定位新的读数".
归档时间: |
|
查看次数: |
9207 次 |
最近记录: |