2012-06-01 27 views
4

我在使用MapQuest提供的Android中使用地圖,但以與Google地圖相同的方式實現,因此適用相同的問題。Android地圖 - 使用佈局作爲疊加項目

對於覆蓋項目,我想使用帶有ImageView和TextView的佈局,因爲TextView將顯示一些信息。問題是我似乎只能顯示一個可繪製的圖層,它必須來自Drawable文件夾。

這裏是將疊加層項目代碼:

private void loadSingleOverlay(TapControlledMap mapViewPassed, String title, String address) 
{ 
    Drawable icon = getResources().getDrawable(R.drawable.marker2); 

    OverlayItem newItem = new OverlayItem(point, title, address); 

    overlay = new ExtendedItemizedOverlay(icon); 
    overlay.addItem(newItem); 

    // add a tap listener 
    overlay.setTapListener(tapListener); 

    List<Overlay> listOfOverlays = mapViewPassed.getOverlays(); 
    listOfOverlays.clear(); 
    listOfOverlays.add(overlay); 
    mapViewPassed.invalidate(); 

} 

我要膨脹的佈局和使用:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/marker_red" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="1" /> 

</RelativeLayout> 

這裏是我要去的結果:

enter image description here

謝謝全部

回答

4

這並不容易。我知道的唯一方法是創建你的視圖(例如帶有背景和文本視圖的佈局),然後將其渲染爲位圖。

public static Bitmap loadBitmapFromView(View v) { 
    DisplayMetrics dm = v.getContext().getResources().getDisplayMetrics(); 
    Bitmap b = Bitmap.createBitmap(Math.round(v.getMeasuredWidth() * dm.density), Math.round(v.getMeasuredHeight() * dm.density), Bitmap.Config.ARGB_8888);     
     Canvas c = new Canvas(b); 
     v.draw(c); 
     return b; 
    } 

然後,您可以添加到您的地圖,如下所示。根據您的標記視圖大小,您將不得不擺脫界限。

Drawable d = new BitmapDrawable(Utils.loadBitmapFromView(yourView)); 
     d.setBounds(-35, -30, Math.round(d.getIntrinsicWidth() * dm.density) - 35, Math.round(d.getIntrinsicHeight() * dm.density) - 30); 
     overlayItem.setMarker(d); 
     overlayListView.add(overlay); 
+0

謝謝,這個效果很好! –