Java 我的位置超出了更改标记的范围

Java 我的位置超出了更改标记的范围,java,android,google-maps,maps,marker,Java,Android,Google Maps,Maps,Marker,如何更改google maps中MyLocationOverlay的默认蓝色动画标记?步骤1:创建MyLocationOverlay的子类 第2步:覆盖drawMyLocation()并根据需要绘制标记。请记住,此方法不仅绘制标记,而且“如果用户的位置移动到屏幕边缘附近,并且在构造函数中为我们提供了一个MapController,我们将滚动以重新居中显示新读数”。Thx@commonware,您将我引向了正确的方向。我花了不少心思在这上面。上的Javadoc是错误的(或过时的),它会扰乱你的大脑

如何更改google maps中MyLocationOverlay的默认蓝色动画标记?

步骤1:创建MyLocationOverlay的子类


第2步:覆盖
drawMyLocation()
并根据需要绘制标记。请记住,此方法不仅绘制标记,而且“如果用户的位置移动到屏幕边缘附近,并且在构造函数中为我们提供了一个
MapController
,我们将滚动以重新居中显示新读数”。

Thx@commonware,您将我引向了正确的方向。我花了不少心思在这上面。上的Javadoc是错误的(或过时的),它会扰乱你的大脑,它会说:

drawMyLocation

此外,如果用户的位置移动到屏幕边缘附近,并且在构造函数中为我们提供了一个MapController,我们将滚动以重新居中新读数

这是完全错误的。首先,类中没有这样的构造函数。其次,drawMyLocation仅在当前位置在viewbounds内或屏幕移动时调用。因此,如果您收到一个新位置,但此时未看到该标记,则不会重新绘制该标记。这通常是一件好事,但如果要在方法中实现mc.animateTo以保持位置居中,则这是一件坏事

第三,启用currentLocation不需要传递MapController,因为您已经拥有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;
  }
}
公共类CurrentLocationOverlay扩展了MyLocationOverlay{
//TODO:使用动态计算?
私有最终静态整数填充\活动\缩放=50;
专用地图控制器;
私有位图标记;
专用点currentPoint=新点();
私有布尔值centerOnCurrentLocation=true;
私人内部高度;
私有整数宽度;
/**
*默认情况下,如果currentLocation靠近当前位置,则此CurrentLocationOverlay将以当前位置为中心
*要动态启用/禁用此功能,请使用{@link#setCenterOnCurrentLocation(布尔值)}。
*
*@param上下文
*@param映射视图
*/
公共CurrentLocationOverlay(上下文上下文,MapView MapView){
超级(上下文,地图视图);
this.mc=mapView.getController();
this.marker=BitmapFactory.decodeResource(context.getResources(),R.drawable.position);
}
@凌驾
受保护的void drawMyLocation(画布、MapView MapView、位置lastFix、地质点myLocation、长时间){
//TODO:在地图视图正确布局后,找到更好的方法获取高度/宽度
如果(this.height==0){
this.height=mapView.getHeight();
this.width=mapView.getWidth();
}
mapView.getProjection().toPixels(myLocation,currentPoint);
drawBitmap(marker,currentPoint.x,currentPoint.y-40,null);
}
@凌驾
公共同步的void onLocation已更改(位置){
super.onLocationChanged(位置);
//只有在启用并且我们处于边界区域时,才能移动到新位置
如果(mc!=null&¢erOnCurrentLocation&&inZoomActiveArea(currentPoint)){
mc.animateTo(getMyLocation());
}
}
私有布尔值inZoomActiveArea(点currentPoint){
if((currentPoint.x>填充\活动\缩放和¤tPoint.x<宽度-填充\活动\缩放)
&&(currentPoint.y>填充\活动\缩放和¤tPoint.y<高度-填充\活动\缩放)){
返回false;
}
返回true;
}
公共void setCenterOnCurrentLocation(布尔值centerOnCurrentLocation){
this.centerOnCurrentLocation=centerOnCurrentLocation;
}
}

我只想把它变成绿色而不是蓝色。我已经在android中找到了资源(动画xml资源和4幅图像)。改变颜色的部分很容易。但是随着编程的进行,我想要完全相同的行为。有没有任何地方可以找到drawMyLocationMethod()的代码?@DArkO:对不起,Google API附加组件不是开源的。关于如何实现这一点,我还有一个想法。我可以用我的版本替换adroid SDK中的资源吗(名称、维度等都保持不变)?@DArkO:只需要创建自己的固件。嗯,文档中说了这一点。但是我从来没有找到一个允许传递MapController的构造函数。我错过什么了吗?