我如何为“你在这里"使用自定义位图?指向 MyLocationOverlay? [英] How can I use a custom bitmap for the "you are here" point in a MyLocationOverlay?

查看:19
本文介绍了我如何为“你在这里"使用自定义位图?指向 MyLocationOverlay?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经翻遍了文档,但一直无法弄清楚这一点.甚至有可能吗?

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

请看这个

推荐答案

看起来正确的机制是扩展 MyLocationOverlay 然后覆盖 drawMyLocation() 保护方法.

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

这篇关于我如何为“你在这里"使用自定义位图?指向 MyLocationOverlay?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆