2010-11-14 53 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; 
    } 
}