我找到了这个解决方案并尝试一下,如何使用传单、闪亮的应用程序在地图上显示路径和距离?
但是当我运行代码时
library(leaflet)
library(osrm)
route = osrmRoute(c(115.6813467,-32.0397559), c(150.3715249,-33.8469759),overview = "simplified")
# route_simple = osrmRoute(c(115.6813467,-32.0397559), c(150.3715249,-33.8469759), overview = 'simplified')
route_summary = osrmRoute(c(115.6813467,-32.0397559), c(150.3715249,-33.8469759), overview = FALSE)
leaflet() %>% addTiles() %>%
addMarkers(c(115.6813467,150.3715249), c(-32.0397559,-33.8469759))%>%
addPolylines(route$lon,route$lat,
label = paste(round(route_summary[1]/60), 'hr - ', round(route_summary[2]), 'km'),
labelOptions = labelOptions(noHide = TRUE))
没有找到任何路线$lon和路线$lat,它给了我这个错误
validateCoords(lng, lat, funcName, mode = "polygon") 中的错误: addPolylines 需要非 NULL 经度/纬度值
你能帮我吗?
尝试使用传单在地图上显示路线
在当前版本的
osrm
中,osrmRoute()
返回一个sf
对象,可以将其传递给leaflet()
:
library(leaflet)
library(osrm)
# osrmRoute() returns an sf object with a LINESTRING,
# also duration and distance atributes ...
route = osrmRoute(c(115.6813467,-32.0397559), c(150.3715249,-33.8469759), overview = "simplified")
route
#> Simple feature collection with 1 feature and 4 fields
#> Geometry type: LINESTRING
#> Dimension: XY
#> Bounding box: xmin: 115.7247 ymin: -34.73311 xmax: 150.3296 ymax: -31.35811
#> Geodetic CRS: WGS 84
#> src dst duration distance geometry
#> src_dst src dst 3029.825 3826.558 LINESTRING (115.7247 -32.05...
# ... which can be directly passed to leaflet() or layer functions as a data parameter;
# and releveant duration & distance values can be accessed through formula notation:
# label = ~ sprintf("%d hr - %d km", round(duration/60), round(distance))
leaflet(route) |>
addTiles() |>
addMarkers(c(115.6813467,150.3715249), c(-32.0397559,-33.8469759)) |>
addPolylines(label = ~ sprintf("%d hr - %d km", round(duration/60), round(distance)),
labelOptions = labelOptions(noHide = TRUE))
创建于 2024-03-12,使用 reprex v2.1.0