# Find walking distance in R using latitude and longitude vectors

Hi, I have a series of houses and a number of train stations. I want to calculate the walking distance from each house (n = 1718) to each station (n = 11). I know Google limits you to 2000 obs per day, so I would do all home obs at 1 station. My data looks like this:

``````Home data:

longitude   latitude
1   -76.27769   36.86308
2   -76.29188   36.87556
3   -76.26982   36.86628
4   -76.27455   36.86894

Station Data
Longitude   Latitude
1   -76.30377   36.85945
2   -76.29490   36.85395
3   -76.28896   36.85156
4   -76.28989   36.84719
5   -76.28579   36.84568
```

```

I found a code like this, but it sums the distance for each house, not the distance for each individual house. I am very new to R ... help!

``````  `distHoras <- function(origin, destination){

origin <- gsub(",", "", origin)
origin <- gsub(" ", "+", origin)
origin <- paste("origins=", origin, sep = "")

destination <- gsub(",", "", destination)
destination <- gsub(" ", "+", destination)
destination <- paste("destination=", paste(destination,
collapse = "|"), sep = "")

mode4url <- paste("mode=", 'walking', sep = "")
lang4url <- paste("language=", 'en-EN', sep = "")
sensor4url <- paste("sensor=", tolower(as.character(FALSE)),
sep = "")
posturl <- paste(origin, destination, mode4url, sensor4url,
sep = "&")
posturl, sep = "")
url_string <- URLencode(url_string)
connect <- url(url_string)
tree <- fromJSON(paste(readLines(connect), collapse = ""))
close(connect)
rapply(tree\$rows,I)
}`
```

```

I am getting output like this

``````distHoras('origin', 'destination')
elements.distance.text elements.distance.value
"1,253 km"               "1252635"
elements.duration.text elements.duration.value
"9 days 8 hours"                "804659"
elements.status
"OK"
```

```
+3

source to share

Something like that??

``````google.dist <- function(from,to,mode="walking") {
require(httr)
require(XML)
origin <- paste(with(from,paste(latitude,longitude,sep=",")),collapse="|")
dest   <- paste(with(to,paste(latitude,longitude,sep=",")),collapse="|")
response <- GET(url,query=list(origins=origin,destinations=dest,mode=mode))
doc      <- content(response,type="text/xml")
status   <- sapply(doc["//row/element/status"],xmlValue)
if(any(status!="OK")) warning("Error Status on some routes")
distance <- sapply(doc["//row/element/distance/value"],xmlValue)
data.frame(expand.grid(to=1:nrow(to),from=1:nrow(from)),distance=as.numeric(distance))
}
#    to from distance
# 1   1    1     3275
# 2   2    1     2494
# 3   3    1     2163
# 4   4    1     2548
# 5   5    1     2212
# 6   1    2     2539
# 7   2    2     2950
# 8   3    2     3288
# 9   4    2     3815
# 10  5    2     4034
# ...
```

```

In this case, the Google Distance Matrix API is used with XML output. Returned data frame of data frames comprises a number of rows `to`

and `from`

the distance between them. The API is documented here . Please pay attention to the terms of use .

+1

source

All Articles