問題描述
我想根據子標記的屬性總和在 Leaflet/Shiny 應用程序中自定義聚集標記的外觀.
I'd like to customize appearance of clustered markers in Leaflet/Shiny application based on sum of an attribute of child markers.
類似于這個問題,它根據孩子的數量制作集群的圖標顏色.如果我想根據地震震級和自定義圖標怎么辦?
It is similar to this problem, which makes icon color of clusters based on count of children. What if I want to customize icon based on the sum of magnitude of earthquakes?
使用純 javascript 應用程序,似乎我應該能夠將自定義屬性設置為單個標記,然后從 iconCreateFunction
訪問它,就像在 這個例子.
With pure javascript application, seems like I should be able to set custom property to individual marker, then access it from iconCreateFunction
, as done in this example.
但是我正在為 R 的傳單添加帶有 addCircleMarkers
和 addMarkers
的標記,并且似乎我不能為正在生成的標記添加任意屬性.下面的代碼有效,但如果我取消注釋兩行(mag = ~mag
和 sum += markers[i].mag;
)
But I am adding marker with addCircleMarkers
and addMarkers
from leaflet for R, and doesn't seem i can add arbitrary attribute to markers being generated. Code below works but it doesn't if i uncomment two lines (mag = ~mag
and sum += markers[i].mag;
)
leaflet(quakes) %>% addTiles() %>% addMarkers(
# mag = ~mag,
clusterOptions = markerClusterOptions(
iconCreateFunction=JS("function (cluster) {
var markers = cluster.getAllChildMarkers();
var sum = 0;
for (i = 0; i < markers.length; i++) {
// sum += markers[i].mag;
sum += 1;
}
return new L.DivIcon({ html: '<div><span>' + sum + '</span></div>'});
}")
)
)
我想過使用 addMarkers
的 label=
選項,然后從 Javascript 中解析它.但是在 JS 的標記簇上使用 getAllChildMarkers()
訪問的標記似乎沒有 label
屬性.
I thought about using label=
option of addMarkers
, and then parse it from Javascript. But the markers accessed with getAllChildMarkers()
on marker cluster in JS does not seem to have label
property.
我還考慮過將數據幀從 R 傳遞到傳單(JS),不知何故,也許 喜歡這個例子,或 this ...?
I also thought about passing a dataframe from R to leaflet(JS), somehow, maybe like this example, or this ...?
推薦答案
找到了我的答案.似乎我可以在 addMarker
的 options=
中放置任意屬性:
Found my answer. Seems like I can put arbitrary property inside options=
in addMarker
:
leaflet(quakes) %>% addTiles() %>% addMarkers(
options = markerOptions(mag = ~mag),
clusterOptions = markerClusterOptions(
iconCreateFunction=JS("function (cluster) {
var markers = cluster.getAllChildMarkers();
var sum = 0;
for (i = 0; i < markers.length; i++) {
sum += Number(markers[i].options.mag);
// sum += 1;
}
return new L.DivIcon({ html: '<div><span>' + sum + '</span></div>'});
}")
)
)
這篇關于傳單R,如何使與兒童統計相關的聚集圖標出現?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!