2011-09-23 45 views
4

我有一個包含靜態圖像(地圖)的ImageView,我想在用戶單擊的地圖上放置一個標記。在靜態圖像上覆蓋一個位置標記圖像(ImageView)

我有一個OnTouchListener註冊,它提供了點擊座標,但我不知道如何繪製在這個位置。欣賞任何想法。

感謝,男


這裏有一個簡單的解決問題的辦法。它的工作原理是在用戶在屏幕上按下的背景圖像上放置一個紅點。請讓我知道是否有人可以看到它的問題。謝謝,米

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

}

回答

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

更多查看我的帖子

how to display map in android with marker

+0

感謝您的迴應。這是使用實際的MapView嗎?我需要避免使用mapview,因爲我希望地圖可以脫機使用。 m – mAndroid

+0

檢查這個帖子http://www.androiddiscuss.com/1-android-discuss/91902.html – Pratik

+0

兄弟你可以幫助這個問題http://stackoverflow.com/questions/29014212/how-to-place-pin -mark-圖像過一個使用圖像 - -ontouchlistener – reegan29