2012-01-06 188 views
0

我有一個拉特,lng和一部分文本的JSON對象。我正在使用ItemizedOverlay將這些位置添加到Google地圖。我使用一個單獨的標記添加了所有不同的OverlayItem,這是一個Drawable對象(res目錄中的一個簡單的png)。android + Google地圖標記

但是,我更喜歡這個標記更有意義,還包括位置文本。檢索文本沒有問題,但是如何將它添加到位置或標記?

我是否需要建立自己的標記,os是否有任何其他對象允許我在地圖上顯示文本以及位置,或者......?任何指針?

+0

也許試試這個: http://stackoverflow.com/questions/6777022/how-to-drawan-overlay-with-buttons-text-and-image-on-a-google-map – goodm 2012-01-06 10:10:46

回答

1

這是完美的解決方案請....
在你的活動,你需要添加下面的參數

geopoint = new GeoPoint((int) (Double.parseDouble(lat) * 1E6), (int)Double.parseDouble(lon) * 1E6)); 
myMapView.getOverlays().add(new DrawableMapOverlay(this,geopoint,R.drawable.map, name)); 

然後DrawableMapOverlay類

import com.google.android.maps.MapView; 
import com.google.android.maps.Overlay; 

public class DrawableMapOverlay extends Overlay { 

    private static final double MAX_TAP_DISTANCE_KM = 3; 
    // Rough approximation - one degree = 50 nautical miles 
    private static final double MAX_TAP_DISTANCE_DEGREES = MAX_TAP_DISTANCE_KM * 0.5399568 * 50; 
    private final GeoPoint geoPoint; 
    private final Context context; 
    private final int drawable; 
    private final String workerName; 
    /** 
    * @param context the context in which to display the overlay 
    * @param geoPoint the geographical point where the overlay is located 
    * @param drawable the ID of the desired drawable 
    */ 
    public DrawableMapOverlay(Context context, GeoPoint geoPoint, int drawable,String workerName) { 
    this.context = context; 
    this.geoPoint = geoPoint; 
    this.drawable = drawable; 
    this.workerName = workerName; 
    } 

    @Override 
    public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) { 
    super.draw(canvas, mapView, shadow); 

    // Convert geo coordinates to screen pixels 
    Point screenPoint = new Point(); 
    mapView.getProjection().toPixels(geoPoint, screenPoint); 
    Paint paint = new Paint(); 
    // Read the image 
    Bitmap markerImage = BitmapFactory.decodeResource(context.getResources(), drawable); 
    paint.setStrokeWidth(1); 
    paint.setARGB(150, 000, 000, 000); 
    paint.setStyle(Paint.Style.STROKE); 
    // Draw it, centered around the given coordinates 
    canvas.drawBitmap(markerImage, 
     screenPoint.x - markerImage.getWidth()/2, 
     screenPoint.y - markerImage.getHeight()/2, null); 
    canvas.drawText(workerName, screenPoint.x- markerImage.getWidth()/2, screenPoint.y - markerImage.getHeight()/2 , paint); 
    return true; 
    } 

    @Override 
    public boolean onTap(GeoPoint p, MapView mapView) { 
    // Handle tapping on the overlay here 
    return true; 
    } 
} 

它是在工作的罰款我項目。