【问题标题】:Change LineString style for leaflet map with R shiny使用 R 闪亮更改传单地图的 LineString 样式
【发布时间】:2015-01-22 15:51:51
【问题描述】:

我在 R 中有一张传单地图,里面有很多 LineStrings。线条默认为蓝色,具有一定的粗细。我想更改线条颜色并使粗细取决于权重变量。我通过读取 geojson 文件来创建地图。将“样式”部分直接添加到 geojson 中的每个线串功能不起作用。这是我的地图 R 代码:

colchoice = "red"
ptl_str = paste("#! function(feature, latlng){
    return L.circleMarker(latlng, {
    radius: feature.properties.radius || 5,
    fillColor: feature.properties.fillColor || '", colchoice, "',    
    color: '#000',
    weight: 1,
    fillOpacity: 0.8
    })
    } !#", sep="")

m <- Leaflet$new()
m$setView(c(38.892682, -77.031681), zoom = 12)
m$geoJson(geojson, 
      onEachFeature = "#! function(feature, layer){
        layer.bindPopup(feature.properties.popup)
      } !#",        
      pointToLayer = ptl_str)
m

这成功地将地图上的点格式化为点。但现在我也想以不同的方式格式化这些行。我很感激任何帮助。谢谢!

【问题讨论】:

    标签: r leaflet shiny geojson


    【解决方案1】:

    前期:我对 R 一无所知,也无法测试我在说什么,但我知道我在 Javascript 中使用 Leaflet 的方法,并且可以告诉你我将如何在 JS 中完成它,所以你可以将它翻译成回复:

    var geoJsonLayer = L.geoJson(data, {
        onEachFeature: function (feature, layer) {
            if (layer instanceof L.Polyline) {
                layer.setStyle({
                    'color': feature.properties.color
                });
            }
        }
    });
    

    这是一个关于 Plunker 的工作示例:http://plnkr.co/edit/g4juen?p=preview

    【讨论】:

    • 感谢 iH8!我会尝试从这里翻译,但是将它放入 R 的部分确实让我很烦恼。
    猜你喜欢
    • 1970-01-01
    • 2018-12-03
    • 1970-01-01
    • 2019-09-26
    • 2018-06-19
    • 2019-09-03
    • 2023-01-11
    • 2016-12-29
    • 2018-03-13
    相关资源
    最近更新 更多