美文网首页
关于百度地图点聚合自定义布局

关于百度地图点聚合自定义布局

作者: 安卓_背包客 | 来源:发表于2020-10-27 09:43 被阅读0次
        val mLayoutInflater = LayoutInflater.from(mContext)
        val inflate = mLayoutInflater.inflate(R.layout.item_baidu_marker, null)

自定义布局中使用LinearLayout 中 android:layout_height 只能使用“wrap_content”
然后BitmapDescriptorFactory.fromView(inflate) 才能使用
下面是百度demo 中抽出的工具类名字,


image.png

把这些类抽出就可以使用
至于显示聚合数需要修改“DefaultClusterRenderer”这个类的常量

//显示聚合常量
    private static final int[] BUCKETS = {2,3,4,5,6,7,8,9,10, 20, 50, 100, 200, 500, 1000};
//多少开始显示聚合  
  private static final int MIN_CLUSTER_SIZE = 2;


    /**
     * Determine whether the cluster should be rendered as individual markers or a cluster.
     */
    protected boolean shouldRenderAsCluster(Cluster<T> cluster) {
        return cluster.getSize() >=MIN_CLUSTER_SIZE;
    }

至于使用方式

//初始化点聚合管理类
mClusterManager = new ClusterManager<MyItem>(this, mBaiduMap);

//ClusterItem接口的实现类
public class MyItem implements ClusterItem {
    LatLng mPosition;
    public MyItem(LatLng position) {
        mPosition = position;
    }
    @Override
    public LatLng getPosition() {
        return mPosition;
    }
    @Override
    public BitmapDescriptor getBitmapDescriptor() {
        return BitmapDescriptorFactory
                .fromResource(R.drawable.icon_gcoding);
    }
}
// 添加Marker点
LatLng llA = new LatLng(39.963175, 116.400244);
LatLng llB = new LatLng(39.942821, 116.369199);
List<MyItem> items = new ArrayList<MyItem>();
items.add(new MyItem(llA));
items.add(new MyItem(llB));
mClusterManager.addItems(items);

我的demo 使用

public class MarkerClusterItem : ClusterItem {
    private var mTitle: String? = null
    private var mPositon: LatLng? = null
    private var mContext: Context? = null

    constructor(
        mContext: Context,
        mTitle: String?,
        mPositon: LatLng?) {
        this.mTitle = mTitle
        this.mPositon = mPositon
        this.mContext = mContext }
    override fun getPosition(): LatLng {
        return mPositon!!
    }

    override fun getBitmapDescriptor(): BitmapDescriptor {
        val mLayoutInflater = LayoutInflater.from(mContext)
        val inflate = mLayoutInflater.inflate(R.layout.item_baidu_marker, null)
        val mTv = inflate.findViewById<TextView>(R.id.tv_item_baidu_marker_title)
        val mIv = inflate.findViewById<ImageView>(R.id.iv_item_baidu_marker_logo)
      //todo do you String
        return BitmapDescriptorFactory.fromView(inflate)
    }
}




   mMapStatus = MapStatus.Builder().target(getLatlngContent(mLatlng)).zoom(15f).build()
        mClusterManger = ClusterManager(this, mBaiDuMap)
 addMarker()
        mClusterManger?.addItems(mListMarkerLatlng)
        mBaiDuMap?.setOnMapLoadedCallback(this)
        mBaiDuMap?.animateMapStatus(MapStatusUpdateFactory.newMapStatus(mMapStatus))
        mBaiDuMap?.apply {
            setOnMapStatusChangeListener(mClusterManger)
            setOnMarkerClickListener(mClusterManger)
        }
        mClusterManger?.apply {
            setOnClusterClickListener {                   
                false
            }
            setOnClusterItemClickListener {
          
                    T.showToast(this@OrderSelectionMapActivity, "内容为空")
                val movePoint = LatLng(it.position.latitude, it.position.longitude)
                mBaiDuMap?.animateMapStatus(
                    MapStatusUpdateFactory.newLatLngZoom(movePoint, 20f)
                false
            }
        }

聚点刷新
          addMarker()
            mClusterManger?.apply {
                addItems(mListMarkerLatlng)
                cluster()
            }
      

相关文章

网友评论

      本文标题:关于百度地图点聚合自定义布局

      本文链接:https://www.haomeiwen.com/subject/wzptvktx.html