【问题标题】:r - add multiple markers to rMapsr - 向 rMaps 添加多个标记
【发布时间】:2015-09-27 00:56:55
【问题描述】:

问题

如何使用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.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 页面上的示例一样:

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,但我是新手,所以还没有完全理解它,一定是做错了什么。

# 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) 显示多个标记一样

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

这是由 writeOGR 命令生成的 GeoJSON 代码,我已经对其进行了验证 at GeoJSONLint

{
"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 ] } }
]}

【问题讨论】:

  • 嗯,apply(unname(coords[, c("stop_lat", "stop_lon")]), 1, l$marker) 血管中的循环怎么样?
  • @lukeA,好主意,但目前它把标记放在稍微错误的位置(但非常接近);我去看看。

标签: r rmaps


【解决方案1】:

我可以使用library(geojsonio) 正确读取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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-06
    • 2013-03-10
    • 2017-04-28
    • 1970-01-01
    • 1970-01-01
    • 2021-10-04
    • 2011-01-28
    • 2017-12-18
    相关资源
    最近更新 更多