【问题标题】:AddPolygons() requires coordinates from SpatialPolygonsDataFrameAddPolygons() 需要来自 SpatialPolygonsDataFrame 的坐标
【发布时间】:2017-05-26 18:19:44
【问题描述】:

我在使用 Leaflet 时遇到问题。

以下函数工作正常,但是当我想添加标记时,我收到一条错误消息,告诉我调用经度和纬度变量。

工作正常:

leaflet(mapdata) %>%
    addPolygons(color = "#444444", weight = 1, smoothFactor = 0.5,
    opacity = 1.0, fillOpacity = 0.5,
    highlightOptions = highlightOptions(color = "white", weight = 2,
    bringToFront = TRUE))

不能正常工作:

leaflet(mapdata) %>%
    addPolygons(color = "#444444", weight = 1, smoothFactor = 0.5,
    opacity = 1.0, fillOpacity = 0.5,
    highlightOptions = highlightOptions(color = "white", weight = 2,
    bringToFront = TRUE),
    addMarkers(data='coordinates', lng = longitude, lat = ~ latitude))

我收到以下错误:

Error: Error in derivePolygons(data, lng, lat, missing(lng), missing(lat), "addPolygons") : 
addPolygons must be called with both lng and lat, or with neither

我使用 GADM 来获取我的地图数据:

mapdata <- getData('GADM', country='NL', level=1)

特定省份的坐标可以通过以下方式找到,但我不知道如何将所有坐标添加到函数中:

head(mapdata@polygons[[1]]@Polygons[[1]]@coords)

         [,1]     [,2]
[1,] 6.501305 53.19841
[2,] 6.504287 53.19749
[3,] 6.504779 53.19761
[4,] 6.514869 53.20009
[5,] 6.522253 53.19825
[6,] 6.523739 53.19736

我找到了答案here。他们告诉我使用以下函数转换我的数据:

mapdata_latlon <- spTransform(mapdata, CRS("+proj=longlat +datum=WGS84"))

但是当我使用转换后的数据时,我得到了同样的错误。

有人可以帮我吗?谢谢。

【问题讨论】:

    标签: r leaflet


    【解决方案1】:

    我解决了。显然,我以错误的方式添加了addMarkers()

    正确代码:

    leaflet(mapdata) %>%
        addPolygons(color = "#444444", weight = 1, smoothFactor = 0.5,
                opacity = 1.0, fillOpacity = 0.5,
                    highlightOptions = highlightOptions(color = "white", weight = 2,
                    bringToFront = TRUE)) %>%
        addMarkers(data = coordinates, lng = ~ longitude, lat = ~ latitude)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-16
      • 2016-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多