首页 > 解决方案 > Mapping a circle radius of a city in Leaflet in R

问题描述

I have a list of cities in R that I want to map a 75 mile radius around in leaflet in r. enter image description here

I know addCircle will map a circle around the given latitude and longitude, but i dont know how to get the lat and long for the cities.

标签: r

解决方案


library(tmaptools)
library(sf)
library(leaflet)
cities <- c("Sacramento", "Santa Clarita")
cities_new <- geocode_OSM(cities)
#           query      lat       lon  lat_min  lat_max   lon_min   lon_max
# 1    Sacramento 38.58106 -121.4939 38.43757 38.68551 -121.5601 -121.3627
# 2 Santa Clarita 34.39166 -118.5426 34.34145 34.49610 -118.6133 -118.3788

cities.sf <- cities_new %>% st_as_sf(coords = c("lon", "lat"), crs = 4326)
cities.buffer <- cities.sf %>% sf::st_buffer( dist = 120700)

leaflet() %>% addTiles() %>%
  addCircleMarkers(data = cities.sf, color = "red") %>%
  addPolygons(data = cities.buffer)

在此处输入图像描述


推荐阅读