I would like to know how do I get the shortest path between two points on a map made by the leaflet package. I have the map done (figure below), but I would like to calculate the shortest path, as well as show this route on the map. It is possible?? The executable code is below.
library(leaflet)
library(geosphere)
#database
df<-structure(list(Properties = c(1,2,3,4,5,6,7,8,9,10), Latitude = c(-23.2, -23.6, -23.9, -23.9, -23.6, -23.5, -23.9, -23.9, -23.6, -23.9),
Longitude = c(-49.6, -49.6, -49.6, -49.4, -49.3, -49.9, -49.3, -49.2, -49.6, -49.9)), class="data.frame",row.names = c(NA, -10L))
#clusters
d<-as.dist(distm(df[,2:1]))
fit.average<-hclust(d,method="average")
clusters<-cutree(fit.average, 4)
df$cluster<-clusters
#Map using leaflet
example=df
getColor <- function(example) {
sapply(example$cluster, function(cluster) {
if(cluster == 1) {
"blue"
} else if(cluster == 2) {
"green"
} else if(cluster == 3) {
"orange"
} else {
"red"
} })
}
icons <- awesomeIcons(
icon = 'ios-close',
iconColor = 'black',
library = 'ion',
markerColor = getColor(example)
)
m=leaflet(example) %>% addTiles() %>%
addAwesomeMarkers(lat=~Latitude, lng = ~Longitude, icon=icons, label=~as.character(cluster))
m
Thank you very much!!
Example that could be
Source:https://gist.github.com/ramnathv/9998388#file-code-r
Source: https://www.liedman.net/leaflet-routing-machine/tutorials/interaction/
See Question&Answers more detail:
os