2009-04-15 4 views

答えて

14

これを行うには、正しいメカニズムはdrawMyLocation()保護されたメソッドをオーバーライドし、その後MyLocationOverlayを拡張することであるように見えます。

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

を変更したときに、矢印を再描画する

mapView.postInvalidate(); 

ビットマップですが、回転はありません – marimaf

2

を、私は仕事にそれを得るためにpreviuosコードにいくつかの変更を加えました:

以下は、「あなたは」とどの方法「あなたは」ポインティングされている場所を示すために矢印を使用しています矢印が間違った方向を指して反対方向に回転していたからです。

は私が

matrix.postRotate(this.getRotation()); 

ため

matrix.postRotate(mOrientation); 

を変更し、最後にコメントを追加しました:それは私が変更することができた

+0

どのようにしてgetRotation()を実装しましたか?ありがとう – marimaf

+0

'MyLocationOverlay'に' getRotation() 'が既に実装されています –

+0

getOrientation()に変更されたようです。 –