【问题标题】:myLocationOverlay change the markermyLocationOverlay 更改标记
【发布时间】:2010-11-14 00:07:28
【问题描述】:

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

【问题讨论】:

    标签: java android google-maps maps marker


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      第 1 步:创建MyLocationOverlay 的子类。

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

      【讨论】:

      • 我只想把它变成绿色而不是蓝色。我已经在 android 中找到了资源(动画 xml 资源和 4 张图像)。改变颜色的部分很容易。但随着编程的进行,我想要完全相同的行为。在哪里可以找到 drawMyLocationMethod() 的代码??
      • @DArkO:抱歉,Google API 插件不是开源的。
      • 我对如何制作这个还有 1 个想法。我可以用我的版本替换 adroid SDK 中的资源吗(名称、尺寸等所有内容都保持不变)?
      • @DArkO:只能创建自己的固件。
      • 嗯,它在文档中这么说。但我从未找到允许传递 MapController 的构造函数。我错过了什么吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-11
      • 2013-10-06
      相关资源
      最近更新 更多