Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overlaying a place marker image over a static image (ImageView)

I've got an ImageView containing a static image (map) and I would like to put a marker on the map where the user clicks.

I have an OnTouchListener registered and it's providing co-ordinates of the click but I don't know how to draw at this location. Appreciate any ideas.

Thanks, m


Here's a simple solution to the problem. It works, places a red dot on the background image where the user presses on the screen. Please let me know if anyone can see a problem with it. Thanks, 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);
    }
}

}

like image 606
mAndroid Avatar asked Nov 04 '22 14:11

mAndroid


1 Answers

you need to convert lat/lng value into the screen pixel using mapview.getProjection.toPixel() method

here is code

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

for more check my post

how to display map in android with marker

like image 193
Pratik Avatar answered Nov 15 '22 04:11

Pratik