2009-04-15 7 views

Répondre

14

Il semble que le mécanisme approprié pour ce faire est d'étendre MyLocationOverlay puis passer outre la méthode protégée drawMyLocation().

qui suit utilise une flèche pour montrer où « vous » êtes et de quelle manière « vous » désignez:

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

j'ai pu changer la bitmap, mais la rotation n'est pas – marimaf

2

J'ai fait quelques modifications au code previuos afin de le faire fonctionner correctement parce que la flèche pointait dans la mauvaise direction et tournait dans la direction opposée.

j'ai changé

matrix.postRotate(mOrientation); 

pour

matrix.postRotate(this.getRotation()); 

et ajouté à la fin:

mapView.postInvalidate(); 

pour redessiner la flèche quand il change

+0

Comment avez-vous implémenté getRotation()? merci – marimaf

+0

'getRotation()' est déjà implémenté dans 'MyLocationOverlay' –

+0

On dirait qu'il a été changé en getOrientation() –

Questions connexes