问题描述
我正在尝试在leaflet
地图上显示几个邮政编码(因此是多边形...).数据以geojson
文件的形式提供此处.我以西雅图的一些邮政编码为例.
I am trying to display several zipcodes (thus polygons...) on a leaflet
map. Data is available as a geojson
file here. I chose as an example some zip codes from Seattle.
我尝试了以下操作(可复制的示例):
I tried the following (reproducible example):
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
参数,并且调用map仅显示leaflet() %>% addTiles()
部分...
I could not figure out how to properly set addGeoJSON
parameters, and calling map only displays the leaflet() %>% addTiles()
part...
对于我是非json高级用户而言,文档太浅了:
Documentation is too light for the non json advanced user that I am:
我应该如何进行?非常感谢您对这个问题的看法
How should I proceed? Thank you very much in advance for your views on this issue
致谢
推荐答案
您只需要不将geojson解析为data.frame,fromJSON(url, FALSE)
You just needed to not parse the geojson to a 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()
也将接受字符串,例如
addGeoJSON()
will also accept a string, e.g.
geojson_str <- paste0(readLines(url), collapse = "")
然后将其传递给addGeoJSON
这篇关于如何使用传单addGeoJSON()函数一次显示多个多边形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!