如何自定义群集选项,以便标记不会被默认的Leaflet markerOptions(标记计数)聚类,而是通过我选择的函数(mean,maximum或whatelse)进行聚类? 对于Java,我可以找到大量的例子,但对于R,我找不到任何东西。 我唯一能找到的就是与之有关的东西 " iconCreateFunction"和" JS()",但我不知道它是否正确以及它是如何工作的。
leaflet(data) %>%
addTiles() %>%
addMarkers(lng=data$lon, lat=data$lat, clusterOptions = list(iconCreateFunction = JS(...
有人能帮助我吗?提前致谢
答案 0 :(得分:1)
回复旧问题但是有些用户仍然觉得这很有用。
您需要将自定义iconCreateFunction
javascript公式传递给markerClusterOptions()
主要挑战是如何将数据传递给标记,以便您可以将公式应用于标记簇中的数据。我试图阅读示例网站上的javascript代码,但由于我不知道js,我只能找到解决方法。
示例网站:http://leaflet.github.io/Leaflet.markercluster/example/marker-clustering-custom.html
用户正在marker[i].number
函数中将数据添加到populate()
。
如果有人知道这项工作如何,请添加我认为比我目前使用的更好的解决方案。
我的解决方法是将数据存储到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;
}
}
亲切的问候, 彼得