myLocationOverlay更改标记

时间:2010-11-14 00:07:29

标签: java android google-maps maps marker

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

2 个答案:

答案 0 :(得分:9)

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;
  }
}

答案 1 :(得分:5)

步骤1:创建MyLocationOverlay的子类。

步骤2:覆盖drawMyLocation()然后根据需要绘制标记。请记住,此方法不仅会绘制标记,而且“如果用户的位置移动到屏幕边缘附近,并且我们在构造函数中被赋予了MapController,我们将滚动以重新定位新的读数”。