how to find distance and time of randomly generated latitude and longitude but gives when i specifically mentioned latitude and longitude

hello i donot understand the problem when i generate random lat and long it didnot give distance and time (reprex below)and when i mention specific lat and long it gives both(reprex also attached) please help to solve the issue.

Lat= function(){
  a = runif(1,29.000000,34.000000)
  
  return(a)
}

 Long= function(){
  b = runif(1,69.000000,72.000000)
  
  return(b)
}
onlineloc=data.frame(Lat=Lat(),Long=Long())
instore=data.frame(Lat=Lat(),Long=Long())
onlineloc
#>       Lat     Long
#> 1 32.5422 71.29198
instore
#>        Lat    Long
#> 1 32.53636 71.9005
library(gmapsdistance)
results = gmapsdistance(origin = "instore", destination = "onlineloc", mode = "driving", key = "") 
results
#> $Time
#> [1] NA
#> 
#> $Distance
#> [1] NA
#> 
#> $Status
#> [1] "PLACE_NOT_FOUND"

Created on 2020-01-23 by the reprex package (v0.3.0)

specifically mentioned both Lat and Long

library(mapsapi)
library(gmapsdistance)
results = gmapsdistance(origin = "31.713661+73.985123", destination = "33.783184+72.723076", mode = "driving", key = "") 
results
#> $Time
#> [1] 13395
#> 
#> $Distance
#> [1] 341800
#> 
#> $Status
#> [1] "OK"

Created on 2020-01-23 by the reprex package (v0.3.0)

There are a few problems with your code:

  • You are passing literal strings (with quotes) as origin and destination parameters not latitude and longitude values.
  • You have to maintain the same format you are using in your last example, apparently you cant provide the values in the form of a data frame.
  • Not always is going to be a valid route among your random origin and destination points so this is always going fail randomly.
library(gmapsdistance)
set.seed(1234)

Lat= function(){
  a = runif(1,29.000000,34.000000)
  
  return(a)
}

Long= function(){
  b = runif(1,69.000000,72.000000)
  
  return(b)
}

onlineloc <- paste0(Lat(), "+", Long())
instore <- paste0(Lat(), "+", Long())

results <- gmapsdistance(origin = instore, destination = onlineloc, mode = "driving", key = Sys.getenv("google_key")) 
results
#> $Time
#> [1] 21513
#> 
#> $Distance
#> [1] 330997
#> 
#> $Status
#> [1] "OK"

Created on 2020-01-23 by the reprex package (v0.3.0)

1 Like

refer to your answer how i can show same lat and long on google map with time and distance please guide and secondly i want to get distance only from results for further use in calculation how i can do it

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.