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

矢印が間違った方向を指しており、反対方向に回転していたため、正しく動作させるために以前のコードにいくつかの変更を加えました。

私が変更され

matrix.postRotate(mOrientation);

為に

matrix.postRotate(this.getRotation());

そして最後に追加しました:

mapView.postInvalidate();

変更時に矢印を再描画する

于 2010-10-01T09:03:53.387 に答える