如何在R语言Leaflet中通过iconCreateFunction缩小图标(附示例)
解决方案
要缩小Leaflet地图中的图标,只需修改iconCreateFunction里L.DivIcon的iconSize参数即可,其余代码保持不变:
- 将原代码中的
iconSize: new L.Point(40, 40)改为更小的数值(比如20, 20,可根据需求调整)。
修改后的完整代码
library(leaflet) data(quakes) quakes <- head(quakes,10) quakes$stations <- as.factor(quakes$stations) map <- leaflet() map <- addTiles(map, group = "OSM (default)") map <- addMarkers(map, quakes$lon, quakes$lat, group = quakes$stations, clusterOptions = markerClusterOptions(maxClusterRadius = 5, transparent=TRUE, singleMarkerMode=TRUE, zoomToBoundsOnClick=FALSE, iconCreateFunction=JS(paste0("function(cluster) { var c = ' marker-cluster-small'; var html = '<div style=\"background-color:rgba(',\"#FF00DB\",')\"><span>' + cluster.getChildCount() + '</div><span>' return new L.DivIcon({html: html, className: 'marker-cluster' + c, iconSize: new L.Point(20, 20) }); }")) ) ) map
说明
- 调整
iconSize的数值可直接控制图标大小,数值越小图标尺寸越小。 - 若需要进一步缩小集群标记的聚合范围,可适当减小
maxClusterRadius的值(原代码为5),此步骤按需选择即可。
内容的提问来源于stack exchange,提问作者Esteban
相关产品推荐
相关产品推荐

