效果图
<img src="https://img-blog.youkuaiyun.com/20160629132557559?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
<span style="font-family: Arial, Helvetica, sans-serif;">自定义Marker布局</span>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#fea531"
android:gravity="center"
android:orientation="horizontal" >
<ImageView
android:id="@+id/img_hotel_image"
android:layout_width="25dp"
android:layout_height="25dp"
android:padding="2dip"
android:scaleType="fitXY"
android:src="@drawable/hotel_pic" />
<TextView
android:id="@+id/tv_hotel_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip"
android:textColor="#ffffff"
android:textSize="14dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/custom_marker_triangle" />
</LinearLayout>
</LinearLayout>
下面这段代码是关键,我们需要把自定义的view转换成Bitmap,MarkerOptions中的icon参数是Bitmap,自定义marker是很常用的
private Bitmap getViewBitmap(View addViewContent) {
addViewContent.setDrawingCacheEnabled(true);
addViewContent.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
addViewContent.layout(0, 0, addViewContent.getMeasuredWidth(), addViewContent.getMeasuredHeight());
addViewContent.buildDrawingCache();
Bitmap cacheBitmap = addViewContent.getDrawingCache();
Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);
return bitmap;
}
加载Marker
latLng = new LatLng(info.getBaiDuLat(), info.getBaiDuLng());
<span style="white-space:pre"> </span>// 图标
<span style="white-space:pre"> </span>View view = LayoutInflater.from(getApplicationContext()).inflate(
<span style="white-space:pre"> </span>R.layout.markerstation_layout, null);
<span style="white-space:pre"> </span>TextView tv_hotel_price = (TextView) view
<span style="white-space:pre"> </span>.findViewById(R.id.tv_hotel_price);
<span style="white-space:pre"> </span>tv_hotel_price.setText(info.getStationName());
<span style="white-space:pre"> </span>BitmapDescriptor markerIcon = BitmapDescriptorFactory
<span style="white-space:pre"> </span>.fromBitmap(getViewBitmap(view));
<span style="white-space:pre"> </span>overlayOptions = new MarkerOptions().position(latLng).icon(markerIcon)
<span style="white-space:pre"> </span>.zIndex(5);
<span style="white-space:pre"> </span>marker = (Marker) (mBaiduMap.addOverlay(overlayOptions));