首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >R-向rMaps添加多个标记

R-向rMaps添加多个标记
EN

Stack Overflow用户
提问于 2015-09-27 00:56:55
回答 1查看 680关注 0票数 0

问题

如何使用rMaps添加多个标记

数据

代码语言:javascript
复制
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"))

示例

使用library(rMaps),我可以创建一个地图并添加一个标记,如Ramnath的github页面上的示例所示:

代码语言:javascript
复制
library(rMaps)

l <- Leaflet$new()
l$setView(c(-37.8602828, 145.079616), zoom=11)
l$tileLayer(provider = "Acetate.terrain")

## add one marker:
l$marker(LatLng = c(-37.81198,144.9557))

但是,我想不出如何从coords数据框架中添加多个标记,而不为每个标记都编写一个l$marker行。

我尝试过使用GeoJSON,但是我是新手,所以我还没有意识到这一点,肯定做错了什么。

代码语言:javascript
复制
# 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")

如果JSON/GeoJSON是可行的话,我很乐意使用它。

期望输出

我希望结果与使用library(leaflet)显示多个标记的结果相同

代码语言:javascript
复制
library(leaflet)
leaflet() %>%
  addProviderTiles("Acetate.terrain") %>%
  setView(lat = -37.8602828, lng = 145.079616, zoom=11) %>%
  addMarkers(data=coords, lat=~stop_lat, lng=~stop_lon)

GeoJSON

下面是从GeoJSON命令生成的writeOGR代码,我已经验证了它的在GeoJSONLint

代码语言:javascript
复制
{
"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 ] } }
]}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-09-27 04:52:43

我可以使用library(geojsonio)正确读取GeoJSON文件,然后使用l$geoJson将标记加载到地图上。

代码语言:javascript
复制
## 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)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32803531

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档