简体   繁体   English

如何在Android谷歌地图上更改群集的颜色?

[英]How can I change the color of the clusters on my android Google map?

Currently I am trying to change the color of cluster icon (the default is blue), but I can't seem to figure it out. 目前我正在尝试更改群集图标的颜色(默认为蓝色),但我似乎无法弄明白。 I have already set up my cluster manager and such, and I can see the icon itself, but it's blue. 我已经设置了我的集群管理器等,我可以看到图标本身,但它是蓝色的。 I am currently using the default Google set up (see code below) to set up my map. 我目前正在使用默认的Google设置(请参阅下面的代码)来设置我的地图。 Any help would be appreciated 任何帮助,将不胜感激

Thanks, 谢谢,

Jacob 雅各

public class MainActivity extends Activity {
    GoogleMap map;
    ClusterManager mClusterManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MapFragment mapFragment = (MapFragment) getFragmentManager()
                .findFragmentById(R.id.map);
        map = mapFragment.getMap();
        setUpClusterer();
    }
    private void setUpClusterer() {
        // Declare a variable for the cluster manager.


        // Position the map.
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(51.503186, -0.126446), 10));

        // Initialize the manager with the context and the map.
        // (Activity extends context, so we can pass 'this' in the constructor.)
        mClusterManager = new ClusterManager<MyItem>(this, map);

        // Point the map's listeners at the listeners implemented by the cluster
        // manager.
        map.setOnCameraChangeListener(mClusterManager);
        map.setOnMarkerClickListener(mClusterManager);

        // Add cluster items (markers) to the cluster manager.
        addItems();
    }

    private void addItems() {

        // Set some lat/lng coordinates to start with.
        double lat = 51.5145160;
        double lng = -0.1270060;

        // Add ten cluster items in close proximity, for purposes of this example.
        for (int i = 0; i < 2; i++) {
            double offset = i / 60d;
            lat = lat + offset;
            lng = lng + offset;
            MyItem offsetItem = new MyItem(lat, lng);
            mClusterManager.addItem(offsetItem);
        }
    }
}


class MyItem implements ClusterItem {
    private final LatLng mPosition;

    public MyItem(double lat, double lng) {
        mPosition = new LatLng(lat, lng);
    }

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

As Verma noted in the comments it is possible to implement a fully customizable icon for your cluster. 正如Verma在评论中指出的那样,可以为您的集群实现完全可自定义的图标。 If all you want is to change the background color in the default icon, this code will do. 如果您只想更改默认图标中的背景颜色,则此代码将执行此操作。

Either way you need to set a renderer for your ClusterManager: 无论哪种方式,您都需要为ClusterManager设置渲染器:

mClusterManager.setRenderer(new CustomClusterRenderer(MainActivity.this, map, mClusterManager));

For CustomClusterRenderer you can extend DefaultClusterRenderer like so: 对于CustomClusterRenderer,您可以像这样扩展DefaultClusterRenderer:

public class CustomClusterRenderer extends DefaultClusterRenderer<MyItem> {

    public CustomClusterRenderer(Context context, GoogleMap map, ClusterManager<MyItem> clusterManager) {
        super(context, map, clusterManager
    }

    @Override
    protected int getColor(int clusterSize) {
        return Color.BLUE // Return any color you want here. You can base it on clusterSize.
    }
}

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

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