shiny - Total distance of route using Leaflet routing machine in rMaps/rCharts -


i produce shiny app asks 2 addresses, maps efficient route, , calculates total distance of route. can done using leaflet routing machine using javascript library, bunch of further calculations distance of route , have embedded in shiny app.

you can produce map using rmaps following demo ramnathv here. i'm not able pull out total distance travelled though can see has been calculated in legend or controller. there exists discussion on how using javascript library - see here. discuss using javascript code:

alert('distance: ' + routes[0].summary.totaldistance); 

here working code rmap. if has ideas how pull out total distance of route , store it, grateful. thank you!

# install dependencies if haven't done library(devtools) install_github("ramnathv/rcharts@dev") install_github("ramnathv/rmaps")  # create function convert address coordinates library(rcurl) library(rjsonio)  construct.geocode.url <- function(address, return.call = "json", sensor = "false") {   root <- "http://maps.google.com/maps/api/geocode/"   u <- paste(root, return.call, "?address=", address, "&sensor=", sensor, sep = "")   return(urlencode(u)) }  ggeocode <- function(address,verbose=false) {   if(verbose) cat(address,"\n")   u <- construct.geocode.url(address)   doc <- geturl(u)   x <- fromjson(doc)   if(x$status=="ok") {     lat <- x$results[[1]]$geometry$location$lat     lng <- x$results[[1]]$geometry$location$lng     return(c(lat, lng))   } else {     return(c(na,na))   } }  # coordinates x <- ggeocode("vancouver, bc") way1 <- ggeocode("645 east hastings street, vancouver, bc") way2 <- ggeocode("2095 commercial drive, vancouver, bc")  # produce map library(rmaps) map = leaflet$new() map$setview(c(x[1], x[2]), 16) map$tilelayer(provider = 'stamen.tonerlite')  mywaypoints = list(c(way1[1], way1[2]), c(way2[1], way2[2]))  map$addassets(   css = "http://www.liedman.net/leaflet-routing-machine/dist/leaflet-routing-machine.css",   jshead = "http://www.liedman.net/leaflet-routing-machine/dist/leaflet-routing-machine.js" )  routingtemplate = "  <script>  var mywaypoints = %s  l.routing.control({   waypoints: [     l.latlng.apply(null, mywaypoints[0]),     l.latlng.apply(null, mywaypoints[1])   ]  }).addto(map);  </script>"  map$settemplate(   afterscript = sprintf(routingtemplate, rjsonio::tojson(mywaypoints)) ) # map$set(width = 800, height = 800) map 

you can create route via google maps api. returned data frame have distance info. sum legs total distance.

library(ggmap) x <- ggeocode("vancouver, bc") way1txt <- "645 east hastings street, vancouver, bc" way2txt <- "2095 commercial drive, vancouver, bc" route_df <- route(way1txt, way2txt, structure = 'route') dist<-sum(route_df[,1],na.rm=t) # total distance in meters # qmap(c(x[2],x[1]), zoom = 12) +    geom_path(aes(x = lon, y = lat),  colour = 'red', size = 1.5, data = route_df, lineend = 'round')  

Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

node.js - How to abort query on demand using Neo4j drivers -