在静态图像(ImageView)上叠加一个地标图像。

5
我有一个包含静态图片(地图)的ImageView,我想在用户单击的位置放置一个标记。
我注册了OnTouchListener,并提供点击位置的坐标,但我不知道如何在该位置绘制。希望能得到任何想法。
谢谢,m
这是解决问题的简单方法。它可以工作,在用户按下屏幕时在背景图像上放置一个红点。如果有人发现问题,请告诉我。谢谢,m
   import android.content.Context;
   import android.graphics.Canvas;
   import android.graphics.Color;
   import android.graphics.Paint;
   import android.util.AttributeSet;
   import android.util.Log;
   import android.view.ViewTreeObserver;
   import android.widget.ImageView;

public class MapPin extends ImageView {
private static final String TAG = "MapPin";

private Paint mPaint; 

private int mBackgroundHeight;
private int mBackgroundWidth; 

private float mPinX;
private float mPinY; 
//private Context mContext; 

private void initMapPinView(){

    mPaint = new Paint();
    mPaint.setColor(Color.RED);
}

public MapPin (Context c){
    super(c);
    initMapPinView();
}

public MapPin (Context c, AttributeSet attrs){
    super(c, attrs);
    initMapPinView();
}

public void setPin (float x, float y){
    mPinX = x; 
    mPinY = y; 
}


@Override
public void onDraw(Canvas canvas){
    super.onDraw(canvas);
    if (mPinX > 0 & mPinY > 0){
        canvas.drawCircle(mPinX, mPinY, 5, mPaint);
    }
}

}

1个回答

2

您需要使用mapview.getProjection.toPixel()方法将经纬度值转换为屏幕像素。

以下是代码:

Point screenPts = new Point();

mapView.getProjection().toPixels(defaultPoint, screenPts);

// ---add the marker---
Bitmap bmp = BitmapFactory.decodeResource(getResources(),R.drawable.cur_loc);
canvas.drawBitmap(bmp, screenPts.x,screenPts.y, null);

更多信息请查看我的帖子

如何在Android中显示带有标记的地图


谢谢回复。这是否使用了实际的MapView?我需要避免使用mapview,因为我希望离线时地图可用。m - mAndroid
兄弟,你能帮忙回答这个问题吗?https://dev59.com/74jca4cB1Zd3GeqP0K7O - reegan29

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接