You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 14:35:56