when i create more than 1 random generation it gives error for 1 its ok


library(mapsapi)
library(gmapsdistance)
Lat= function(){
  a = runif(1,29.00,34.00)
  round(a,digits = 2)
  return(a)
}

Long= function(){
  b = runif(1,69.00,72.00)
  round(b,digits = 2)
  return(b)
}
onlineloc=paste0(LAT=Lat(),"+",LONG=Long())
onlineloc
#> [1] "29.3500238771085+69.3826255111489"

instore=paste0(LAT=Lat(),"+",LONG=Long())
instore
#> [1] "33.1250498138834+70.6069283250254"

mapdist(instore,onlineloc, mode = "driving",key="")
# A tibble: 1 x 9
  from                           to                                 m    km miles seconds minutes hours mode  
  <chr>                          <chr>                          <int> <dbl> <dbl>   <int>   <dbl> <dbl> <chr> 
1 29.0280035613105+69.248907341~ 33.1699386690743+70.71592473~ 763093  763.  474.   50734    846.  14.1 drivi~

Created on 2020-02-09 by the reprex package (v0.3.0)

Hi,

Could you please show us where the error occurs? It seems the functions are working properly. It might be the API that's the problem (I can't test it because I don't have a key).

Also, your code requires the ggmap package to run the mapdist function while the two libraries provided are not needed.

PJ

a function definition for this is missing... are you wrapping gmapsdistance() ?

when i generate 5 generations it gave error

library(ggmap)
#> Loading required package: ggplot2
#> Google's Terms of Service: https://cloud.google.com/maps-platform/terms/.
#> Please cite ggmap if you use it! See citation("ggmap") for details.
library(mapsapi)
library(gmapsdistance)
Lat= function(){
  a = runif(5,29.00,34.00)
  round(a,digits = 1)
  return(a)
}

Long= function(){
  b = runif(5,69.00,72.00)
  round(b,digits = 1)
  return(b)
}
onlineloc=data.frame(LAT=Lat(),LONG=Long())
onlineloc
#>        LAT     LONG
#> 1 32.98012 71.71839
#> 2 33.86351 71.77769
#> 3 33.67363 71.93513
#> 4 32.00375 70.08050
#> 5 33.45341 71.27073
instore=data.frame(LAT=Lat(),LONG=Long())
instore
#>        LAT     LONG
#> 1 29.99904 71.81012
#> 2 29.90679 69.31310
#> 3 31.07845 69.22880
#> 4 29.05070 70.35449
#> 5 33.63937 70.28384

mapdist(instore,onlineloc, mode = "driving",key="A")
#> Error in mapdist(instore, onlineloc, mode = "driving", key = "A"): is.character(from) is not TRUE
#>Error: All columns in a tibble must be 1d or 2d objects:
#>Column `m` is NULL
#>Column `seconds` is NULL
#>Call `rlang::last_error()` to see a backtrace```

<sup>Created on 2020-02-10 by the [reprex package](https://reprex.tidyverse.org) (v0.3.0)</sup>

Hi,

I couldn't test it (no api key here) but I think this might work:

library(ggmap)

#Generate random locations (as strings)
randomLoc = function(n = 1){
  paste0(runif(n,29.00,34.00), #LAT
         "+", 
         runif(5,69.00,72.00)) #LONG
}

#See disctance between locations
mapdist(randomLoc(5), randomLoc(5), mode = "driving",key="A")

The thing is that the locations are now in string format, as you did with the single example. In the other case, you did not merge the LAT and LONG and thus that's what I think produced the error.

Let me know if it works!
PJ

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