2009-04-15 51 views

回答

14

看起來正確的機制來做到這一點是延長然後MyLocationOverlay覆蓋drawMyLocation()受保護方法。

下使用的箭頭以顯示其中「你」是和方式的「你」都指向:

package com.example; 

import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Canvas; 
import android.graphics.Matrix; 
import android.graphics.Point; 
import android.location.Location; 
import com.google.android.maps.GeoPoint; 
import com.google.android.maps.MapView; 
import com.google.android.maps.MyLocationOverlay; 

public class MyCustomLocationOverlay extends MyLocationOverlay { 
    private Context mContext; 
    private float mOrientation; 

    public MyCustomLocationOverlay(Context context, MapView mapView) { 
     super(context, mapView); 
     mContext = context; 
    } 

    @Override 
    protected void drawMyLocation(Canvas canvas, MapView mapView, Location lastFix, GeoPoint myLocation, long when) { 
     // translate the GeoPoint to screen pixels 
     Point screenPts = mapView.getProjection().toPixels(myLocation, null); 

     // create a rotated copy of the marker 
     Bitmap arrowBitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.arrow_green); 
     Matrix matrix = new Matrix(); 
     matrix.postRotate(mOrientation); 
     Bitmap rotatedBmp = Bitmap.createBitmap(
      arrowBitmap, 
      0, 0, 
      arrowBitmap.getWidth(), 
      arrowBitmap.getHeight(), 
      matrix, 
      true 
     ); 
     // add the rotated marker to the canvas 
     canvas.drawBitmap(
      rotatedBmp, 
      screenPts.x - (rotatedBmp.getWidth()/2), 
      screenPts.y - (rotatedBmp.getHeight()/2), 
      null 
     ); 
    } 

    public void setOrientation(float newOrientation) { 
     mOrientation = newOrientation; 
    } 
} 

+0

我能夠改變位圖,但旋轉不是working – marimaf 2011-10-16 22:53:12

2

我爲了得到它的工作作出了previuos代碼進行一些修改正確的原因是箭頭指向錯誤的方向並朝相反的方向旋轉。

我改變

matrix.postRotate(mOrientation); 

matrix.postRotate(this.getRotation()); 

,並添加到末尾:

mapView.postInvalidate(); 

重繪箭頭時,它改變

+0

你是如何實現getRotation()的?謝謝 – marimaf 2011-10-16 22:44:45

+0

'getRotation()'已經在'MyLocationOverlay'中實現了。 – 2012-10-25 13:36:42

+0

好像它改成了getOrientation() – 2012-10-26 09:16:37

相關問題