2010-11-14 52 views

回答

6

步骤#1:创建一个MyLocationOverlay的子类。

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

+0

我只是想让它变成绿色而不是蓝色。我已经在android中找到资源(动画xml资源和4张图片)。改变颜色的部分很容易。但随着编程的进行,我想要完全相同的行为。有什么地方可以找到drawMyLocationMethod()的代码? – DArkO 2010-11-15 23:53:38

+1

@DAKO:对不起,但Google API插件不是开源的。 – CommonsWare 2010-11-16 00:09:01

+0

我还有1个关于如何做到这一点的想法。我可以用我的版本替换adroid SDK中的资源吗(类似名称,尺寸保持不变)? – DArkO 2010-11-16 14:48:43

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