2015-09-27 101 views
0

問題的R - 添加多個標記,以rMaps

你如何使用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)) 

但我無法弄清楚如何從012中添加多個標記數據幀,而不寫入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) 

marker_example

GeoJSON的

同樣在這裏是GeoJSON代碼,是一個從writeOGR命令產生的,我已經驗證它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 ] } } 
]} 
+0

嗯怎麼樣在'應用靜脈循環(unname(COORDS [C( 「stop_lat」, 「stop_lon」)),1,L $標記)'? – lukeA

+0

@lukeA,好主意,但此刻它將標記放置在*稍微*錯誤的地方(但非常接近);我會看看它。 – tospig

回答

0

我可以利用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) 

markers plotted with geoJson