简体   繁体   English

谷歌地图自定义标记图标与 Android 上的聚类

[英]Google maps custom marker icon with clustering on Android

I have implemented the code of Google Maps Clustering我已经实现了Google Maps Clustering的代码

This is the code in my activity这是我活动中的代码

private void setUpClusterer() {
    mClusterManager = new ClusterManager<StoreItem>(this, mMap);
    mMap.setOnCameraChangeListener(mClusterManager);
    mMap.setOnMarkerClickListener(mClusterManager);
}

public void addItems(List<Store> stores) {
    for (Store store : stores) {
        mClusterManager.addItem(new StoreItem(store.getImage(), store.getLocation().getLatitude(), store.getLocation().getLongitude()));
    }
}

private void removeAllItems() {
    mClusterManager.clearItems();
}

This is the StoreItem Class这是 StoreItem 类

public class StoreItem implements ClusterItem {

    private String url;
    private final LatLng mPosition;

    public StoreItem(String url, double lat, double lng) {
        this.url = url;
        mPosition = new LatLng(lat, lng);
    }

    @Override
    public LatLng getPosition() {
        return mPosition;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }
}

Now, I would like to to change the marker icons via the url parameter in the StoreItem class.现在,我想通过 StoreItem 类中的 url 参数更改标记图标。

How can I do this?我该怎么做?

Consider overriding onBeforeClusterRendered .考虑覆盖onBeforeClusterRendered Something like will work:类似的东西会起作用:

BitmapDescriptor descriptor = BitmapDescriptorFactory.fromBitmap(here_goes_your_bitmap);
markerOptions.icon(descriptor);

Keep in mind that code is just for sample.请记住,代码仅用于示例。 You have to add text to bitmap manually and add some caching mechanism for real use.您必须手动将文本添加到位图并添加一些缓存机制以供实际使用。 You can see source code of DefaultClusterRenderer for sample.您可以查看示例的DefaultClusterRenderer 的源代码。

you need to @override method onBeforeClusterItemRendered() Of DefaultClusterRenderer class get iconurl in onBeforeClusterItemRendered set to markerOptions please find below code:您需要@override 方法 onBeforeClusterItemRendered() 的 DefaultClusterRenderer 类获取 onBeforeClusterItemRendered 中的 iconurl 设置为 markerOptions 请找到以下代码:

private class CustomMapClusterRenderer<T extends ClusterItem> extends DefaultClusterRenderer<T> {
        CustomMapClusterRenderer(Context context, GoogleMap map, ClusterManager<T> clusterManager) {
            super(context, map, clusterManager);
        }

        @Override
        protected boolean shouldRenderAsCluster(Cluster<T> cluster) {
            //start clustering if 2 or more items overlap
            return cluster.getSize() >= Constants.MINIMUM_CLUSTER_SIZE;
        }

        @Override
        protected void onBeforeClusterItemRendered(T item,
                                                   MarkerOptions markerOptions) {
            ClusterMarkerItem markerItem = (ClusterMarkerItem) item;
            markerOptions.icon(BitmapDescriptorFactory.fromPath(markerItem.getURL()));
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM