gpt4 book ai didi

java - myLocationOverlay 更改标记

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:42:20 25 4
gpt4 key购买 nike

如何更改谷歌地图中 MyLocationOverlay 的默认蓝色动画标记?

最佳答案

谢谢@CommonsWare,你引导我朝着正确的方向前进。花了相当多的时间来摆弄这个。 http://code.google.com/android/add-ons/google-apis/reference/com/google/android/maps/MyLocationOverlay.html 上的 Javadoc只是错误的(或过时的)并且会扰乱你的大脑,它说:

drawMyLocation

Also, if the user's position moves near the edge of the screen, and we've been given a MapController in our constructor, we'll scroll to recenter the new reading.

这是完全错误的。首先,类中没有这样的构造函数。其次,drawMyLocation 仅在当前 Location 在视界内或屏幕移动时才会被调用。因此,如果您收到一个新位置并且此时没有看到标记,则不会重新绘制它。这在一般情况下是一件好事,但如果您想在方法中实现 mc.animateTo 以保持位置居中,这将是一件坏事。

第三,您不需要传递 MapController 来启用 currentLocation,因为您已经拥有 mapView 并从中获取 Controller ..

所以我最终编写了自己的类,一旦位置到达 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;
}
}

关于java - myLocationOverlay 更改标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4175432/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com