传单R,如何使与儿童统计相关的聚集图标出现?

leaflet R, how to make appearance of clustered icon related to statistics of the children?(传单R,如何使与儿童统计相关的聚集图标出现?)
本文介绍了传单R,如何使与儿童统计相关的聚集图标出现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我想根据子标记的属性总和在 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 的传单添加带有 addCircleMarkersaddMarkers 的标记,并且似乎我不能为正在生成的标记添加任意属性.下面的代码有效,但如果我取消注释两行(mag = ~magsum += 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>'});

  }")

  )
)

我想过使用 addMarkerslabel= 选项,然后从 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 ...?

推荐答案

找到了我的答案.似乎我可以在 addMarkeroptions= 中放置任意属性:

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,如何使与儿童统计相关的聚集图标出现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How do I can get a text of all the cells of the table using testcafe(如何使用 testcafe 获取表格中所有单元格的文本)
node_modules is not recognized as an internal or external command(node_modules 未被识别为内部或外部命令)
How can I create conditional test cases using Protractor?(如何使用 Protractor 创建条件测试用例?)
PhantomJS and clicking a form button(PhantomJS 并单击表单按钮)
Clicking #39;OK#39; on alert or confirm dialog through jquery/javascript?(在警报上单击“确定或通过 jquery/javascript 确认对话框?)
QunitJS-Tests don#39;t start: PhantomJS timed out, possibly due to a missing QUnit start() call(QunitJS-Tests 不启动:PhantomJS 超时,可能是由于缺少 QUnit start() 调用)