2017-08-06 117 views
1

我們可以自定義標記圖標谷歌地圖嗎? 我不想只是簡單地改變圖標位圖(我知道該怎麼做) 我想改變圖標的​​方式就像我有一個XML佈局(有一個ImageView和TextView),我想膨脹這個XML (如自定義信息窗口)。但我想使它成爲標記圖標,所以我可以通過編碼來設置圖像和文本 請注意,我不想要信息窗口,我希望它是標記自定義標記圖標谷歌地圖android

回答

0

您可以創建layour並將該佈局轉換爲Drawable您可以將該繪圖添加到您標記的圖標中。

您可以創建你的佈局是這樣,

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

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

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="20dp" 
     android:layout_marginRight="20dp" 
     android:layout_marginTop="2dp" 
     android:text="Name" 
     android:textSize="16sp"/> 

</LinearLayout> 

以下方法給您的位圖從佈局,

private Bitmap getBitmapFromLayout(int layout) { 

    LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View view = layoutInflater.inflate(layout, null); 

    view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 
     MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); 
    view.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight()); 

    view.buildDrawingCache(); 

    Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), 
      Bitmap.Config.ARGB_8888); 

    Canvas canvas = new Canvas(bitmap); 
    canvas.drawColor(Color.WHITE, PorterDuff.Mode.SRC_IN); 

    Drawable drawable = view.getBackground(); 
    if (drawable != null) 
     drawable.draw(canvas); 

    view.draw(canvas); 

    return bitmap; 
} 

可以在這樣的地圖標記圖標添加的位圖,

mMap.addMarker(new MarkerOptions().icon(BitmapDescriptorFactory.fromBitmap(getBitMapFromLayout([your layout id])))); 
+0

如果我這樣做,我不能設置文本和圖像編程 –

+0

是的,你可以設置任何視圖在你的佈局不僅僅是文字或圖像。 –

+0

添加您的崩潰日誌。 –