百度地图,自定义Marker

本文介绍了如何在百度地图中实现自定义Marker,通过将自定义View转化为Bitmap,设置为MarkerOptions的icon,来达到个性化Marker的效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

效果图
<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));
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值