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 canv开发者_如何学Goas){
super.onDraw(canvas);
if (mPinX > 0 & mPinY > 0){
canvas.drawCircle(mPinX, mPinY, 5, mPaint);
}
}
}
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
精彩评论