r - 在传单簇选项中定义函数

标签 r leaflet

如何自定义聚类选项,以便标记不是按默认的 Leaflet markOptions(标记计数)聚类,而是按我选择的函数(平均值、最大值或其他)聚类? 对于 Java,我可以找到大量示例,但对于 R,我找不到任何东西。 我唯一能找到的就是与 “iconCreateFunction”和“JS()”,但我不知道它是否正确以及它是如何工作的..

leaflet(data) %>%
addTiles() %>%
addMarkers(lng=data$lon, lat=data$lat, clusterOptions = list(iconCreateFunction = JS(...

有人可以帮助我吗?提前致谢

最佳答案

回复旧问题,但有些用户仍然可能会发现这很有用。 您需要将自定义 iconCreateFunction javascript 公式传递给 markerClusterOptions() 主要挑战是如何将数据传递给标记,以便可以将公式应用于标记簇中的数据。我尝试阅读示例网站上的 javascript 代码,但由于我不知道 js,所以我只能找到解决方法。 示例网站:http://leaflet.github.io/Leaflet.markercluster/example/marker-clustering-custom.html 用户正在 populate() 函数中将数据添加到 marker[i].number 中。 如果有人知道这是如何工作的,请添加您的解决方案,我认为该解决方案会比我当前使用的更好。

我的解决方法是将数据存储到 addMarkers(... , title=myData, ...)addCircleMarkers(... , Weight=myData , ...)

library(leaflet)
# sample data to pass to markers
myData <- sample(x=1:1000, size=1000, replace=TRUE)
# add some NaN which may occur in real data
myData[sample(x=1:1000, size=100, replace=FALSE)] <- NaN
circle.colors <- sample(x=c("red","green","gold"),size=1000,replace=TRUE)

avg.formula = 
"function (cluster) {
var markers = cluster.getAllChildMarkers();
var sum = 0;
var count = 0;
var avg = 0;
var mFormat = ' marker-cluster-';
for (var i = 0; i < markers.length; i++) {
if(markers[i].options.weight != undefined){
sum += markers[i].options.weight;
count += 1;
}
}
avg = Math.round(sum/count);
if(avg<333) {mFormat+='small'} else if (avg>667){mFormat+='large'}else{mFormat+='medium'};
return L.divIcon({ html: '<div><span>' + avg + '</span></div>', className: 'marker-cluster'+mFormat, iconSize: L.point(40, 40) });
}"

# in the above we loop through every marker in cluster access the options.weight
# which is our data, if data is not undefined (not NA or NaN) then we sum data
# and count occurrence
# at the end of code we check if average is more/less to assign default
# marker icons marker-cluster-small marker-cluster-medium marker-cluster-large
# for green yellow red respectively

# stroke = FALSE is a must if you store data in weights !!!
leaflet(quakes) %>% addTiles() %>%
 addCircleMarkers(lng=~long,lat=~lat,radius=10,stroke=FALSE,fillOpacity=0.9,
                   fillColor = circle.colors,weight=myData,
                   popup=as.character(myData),
                   clusterOptions = markerClusterOptions(iconCreateFunction=JS(avg.formula)))

对于您需要调整的任何其他自定义公式

for (var i = 0; i < markers.length; i++) {
if(markers[i].options.weight != undefined){
sum += markers[i].options.weight;
count += 1;
}
}

亲切的问候, 彼得

关于r - 在传单簇选项中定义函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32605681/

相关文章:

javascript - 传单上没有显示 map

r - R在导入数据时如何确定因子变量的默认级别排序?

r - r中如何将长表转换为宽表?

R:在 r 中创建具有特定相关性的数据集

javascript - 更新 leafletJS 中的 latlng 对象字段

node.js - 如何将坐标从 node.js 服务器发送到 leaflet.js 客户端

leaflet - Nuxt Leaflet,更改图 block 层请求不正确的图 block

r - 使用 R 中的传单库绘制跨越国际日期变更线的路线

r - ggplot2 绘图 : Change Text in Case it Exceeds the Plot Window when Exporting as PNG

r - 无法加载 tidyverse 库