2

我已经倾注了文档,但无法弄清楚这一点。甚至可能吗?

请看这个

4

2 回答 2

15

看起来这样做的正确机制是扩展 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;
    }
}

于 2009-04-15T21:40:00.417 回答
2

我对 previuos 代码进行了一些更改,以使其正常工作,因为箭头指向错误的方向并朝相反的方向旋转。

我变了

matrix.postRotate(mOrientation);

为了

matrix.postRotate(this.getRotation());

并添加到最后:

mapView.postInvalidate();

当箭头改变时重绘箭头

于 2010-10-01T09:03:53.387 回答