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

前端 未结 1 1652
無奈伤痛
無奈伤痛 2021-01-27 09:24

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 c

相关标签:
1条回答
  • 2021-01-27 09:40

    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() will also accept a string, e.g.

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

    then pass that to addGeoJSON

    0 讨论(0)
提交回复
热议问题