r-向RMAP添加多个标记
问题 如何使用添加多个标记 数据r-向RMAP添加多个标记,r,rmaps,R,Rmaps,问题 如何使用添加多个标记 数据 coords <- structure(list(stop_id = 19841:19843, stop_name = c("Flagstaff Railway Station (Melbourne City)", "Melbourne Central Railway Station (Melbourne City)", "Parliament Railway Station (Melbourne City)" ), stop_lat = c(-37.8
coords <- structure(list(stop_id = 19841:19843, stop_name = c("Flagstaff Railway Station (Melbourne City)",
"Melbourne Central Railway Station (Melbourne City)", "Parliament Railway Station (Melbourne City)"
), stop_lat = c(-37.8119813073807, -37.8099387667386, -37.8110540555305
), stop_lon = c(144.955653760429, 144.962593535096, 144.972910916416
)), .Names = c("stop_id", "stop_name", "stop_lat", "stop_lon"
), sorted = "stop_id", row.names = 17:19, class = c("data.table",
"data.frame"))
但是我不知道如何从coords
数据帧添加多个标记,而不为每个标记写l$marker
行
我尝试过使用GeoJSON
,但我对这一点还不熟悉,所以还没有意识到这一点,肯定是做错了什么
# library(rgdal)
# coords.sp <- SpatialPointsDataFrame(coords[,.(stop_lon, stop_lat)], coords[,.(stop_id, stop_name)])
# writeOGR(obj=coords.sp, dsn='coords.geojson', layer='OGRGeoJSON', driver='GeoJSON')
# gj <- readOGR("./coords.geojson", layer="OGRGeoJSON")
# l$geoJson(gj)
# l$geoJson("./coords.geojson")
GeoJSON
下面是从writeOGR
命令生成的GeoJSON代码,我已经对其进行了验证
我可以使用来正确读取GeoJSON文件,然后使用l$GeoJSON
将标记加载到地图上
## create spatial object and save as GeoJSON
# library(rgdal)
coords.sp <- SpatialPointsDataFrame(coords[,.(stop_lon, stop_lat)], coords[,.(stop_id, stop_name)])
writeOGR(obj=coords.sp, dsn='coords.geojson', layer='OGRGeoJSON', driver='GeoJSON')
l <- Leaflet$new()
l$setView(c(-37.8602828, 145.079616), zoom=11)
l$tileLayer(provider = "Acetate.terrain")
## Read the GeoJSON data
library(geojsonio)
gj <- geojson_read("./coords.geojson")
l$geoJson(gj)
##创建空间对象并另存为GeoJSON
#图书馆(rgdal)
coords.sp Hm关于应用(unname(coords[,c(“stop_lat”,“stop_lon”))),1,l$marker)静脉中的循环如何?@lukeA,好主意,但目前它将标记放在了稍微错误的位置(但非常接近);我会调查一下的。
library(leaflet)
leaflet() %>%
addProviderTiles("Acetate.terrain") %>%
setView(lat = -37.8602828, lng = 145.079616, zoom=11) %>%
addMarkers(data=coords, lat=~stop_lat, lng=~stop_lon)
{
"type": "FeatureCollection",
"features": [
{ "type": "Feature", "id": 1, "properties": { "stop_id": 19841, "stop_name": "Flagstaff Railway Station (Melbourne City)" }, "geometry": { "type": "Point", "coordinates": [ 144.955653760428987, -37.811981307380698 ] } },
{ "type": "Feature", "id": 2, "properties": { "stop_id": 19842, "stop_name": "Melbourne Central Railway Station (Melbourne City)" }, "geometry": { "type": "Point", "coordinates": [ 144.962593535096005, -37.809938766738597 ] } },
{ "type": "Feature", "id": 3, "properties": { "stop_id": 19843, "stop_name": "Parliament Railway Station (Melbourne City)" }, "geometry": { "type": "Point", "coordinates": [ 144.972910916415998, -37.811054055530498 ] } }
]}
## create spatial object and save as GeoJSON
# library(rgdal)
coords.sp <- SpatialPointsDataFrame(coords[,.(stop_lon, stop_lat)], coords[,.(stop_id, stop_name)])
writeOGR(obj=coords.sp, dsn='coords.geojson', layer='OGRGeoJSON', driver='GeoJSON')
l <- Leaflet$new()
l$setView(c(-37.8602828, 145.079616), zoom=11)
l$tileLayer(provider = "Acetate.terrain")
## Read the GeoJSON data
library(geojsonio)
gj <- geojson_read("./coords.geojson")
l$geoJson(gj)