如何使用传单 addGeoJSON() 函数一次显示多个多边形?

How to display multiple polygons at once using leaflet addGeoJSON() function?

我正在尝试在 leaflet 地图上显示多个邮政编码(因此是多边形...)。数据以 geojson 文件 here 的形式提供。我选择了西雅图的一些邮政编码作为示例。

我尝试了以下(可重现的示例):

library(jsonlite) 
library(leaflet)  
url <- "https://raw.githubusercontent.com/openseattle/seattle-boundaries/master/data/zip-codes.geojson"
geojson <- fromJSON(url) 
map <- leaflet() %>% addTiles() %>% addGeoJSON(geojson)
map

我不知道如何正确设置 addGeoJSON 参数,调用地图只显示 leaflet() %>% addTiles() 部分...

文档对于我这样的非 json 高级用户来说太简单了:

geojson: a GeoJSON list, or character vector of length 1

我该如何进行?非常感谢您对这个问题的看法

此致

您只需要不将 geojson 解析为 data.frame、fromJSON(url, FALSE)

library(jsonlite) 
library(leaflet)  
url <- "https://raw.githubusercontent.com/openseattle/seattle-boundaries/master/data/zip-codes.geojson"
geojson <- fromJSON(url, simplifyVector = FALSE) 
leaflet() %>% 
  addTiles() %>% 
  addGeoJSON(geojson) %>% 
  setView(lng = -122.2, lat = 47.6, zoom = 10)

addGeoJSON() 也将接受一个字符串,例如

geojson_str <- paste0(readLines(url), collapse = "")

然后将其传递给 addGeoJSON