How can I use a custom bitmap for the “you are here” point in a MyLocationOverlay?

ぃ、小莉子 提交于 2019-12-17 10:57:16

问题


I've poured over the docs and haven't been able to figure this out. Is it even possible?

Please see this


回答1:


It looks like the correct mechanism to do this is to extend MyLocationOverlay then override the drawMyLocation() protected method.

The following uses an arrow to show where "you" are and which way "you" are pointing:

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;
    }
}




回答2:


I made a few changes to the previuos code in order to get it to work properly because the arrow was pointing to the wrong direction and rotating in the opposite direction.

I changed

matrix.postRotate(mOrientation);

for

matrix.postRotate(this.getRotation());

and added to the end:

mapView.postInvalidate();

to redraw the arrow when it changes



来源:https://stackoverflow.com/questions/753793/how-can-i-use-a-custom-bitmap-for-the-you-are-here-point-in-a-mylocationoverla

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!