如何使用ui-leaflet和Markercluster来改变markercluster的颜色?

时间:2016-12-30 02:47:33

标签: javascript angularjs leaflet leaflet.markercluster ui-leaflet

我必须创建一个非默认的markerCluster颜色。我已经检查了API,看起来他们建议在创建之后修改divIcon(我相信,我正在学习Leaflet,ui-leaflet,MarkerCluster所有这些)同样的事情:

var markers = L.markerClusterGroup({
iconCreateFunction: function(cluster) {
    return L.divIcon({ html: '<b>' + cluster.getChildCount() + '</b>' });
}
});

https://github.com/Leaflet/Leaflet.markercluster

我填充数据的方式是创建,更接近于此:

layers: {
                baselayers: {
                    osm: {
                        name: 'OpenStreetMap',
                        type: 'xyz',
                        url: 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
                        layerOptions: {
                            subdomains: ['a', 'b', 'c'],
                            attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors',
                            continuousWorld: true
                        }
                    },
                    cycle: {
                        name: 'OpenCycleMap',
                        type: 'xyz',
                        url: 'http://{s}.tile.opencyclemap.org/cycle/{z}/{x}/{y}.png',
                        layerOptions: {
                            subdomains: ['a', 'b', 'c'],
                            attribution: '&copy; <a href="http://www.opencyclemap.org/copyright">OpenCycleMap</a> contributors - &copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors',
                            continuousWorld: true
                        }
                    }
                },
                overlays: {
                    hillshade: {
                        name: 'Hillshade Europa',
                        type: 'wms',
                        url: 'http://129.206.228.72/cached/hillshade',
                        visible: true,
                        layerOptions: {
                            layers: 'europe_wms:hs_srtm_europa',
                            format: 'image/png',
                            opacity: 0.25,
                            attribution: 'Hillshade layer by GIScience http://www.osm-wms.de',
                            crs: L.CRS.EPSG900913
                        }
                    },
                    cars: {
                        name: 'Cars',
                        type: 'markercluster',
                        visible: true
                    }
                }
            },
            markers: {
                m1: {
                    lat: 42.20133,
                    lng: 2.19110,
                    layer: 'cars',
                    message: "I'm a moving car"
                },

我从一个类似的(和分叉的中断)网站偷走了: https://github.com/tombatossals/angular-leaflet-directive/blob/master/examples/0216-layers-overlays-markercluster-example.html 我试图添加一个单独的颜色的图标属性无济于事。当多个类型聚集在一起时,我试图改变颜色。任何帮助将不胜感激。

提前致谢。

1 个答案:

答案 0 :(得分:0)

所以最重要的问题是我对如何使用divIcon的理解是有缺陷的。我假设我需要在实例化后更改颜色,这是不正确的。只需将代码添加到我的初始服务中就可以了:

               cars: {
                    name: 'Cars',
                    type: 'markercluster',
                    visible: true,
                    layerOptions:{
                          iconCreateFunction: function(cluster) {
                                return L.divIcon({ html: '<b>' +cluster.getChildCount() + '</b>' });

                    }
                }

我没有意识到他们提供的api功能实际上只是修改现有对象(更准确地说是更改对象的实例化)。如果要实例化它,只需在此layerOptions键下添加变量名称,只需在API上找到变量即可。