Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to calculate distance with mulitiple coordinates using mapdist ? and read Chinese

Tags:

r

I am trying to use mapdist function in ggmap package to calculate distance and walking ,driving speed between many places. I have checked the help file , it appears that coordinate can be accepted by mapdist ,but can't find out how to input multiple coordinates ? the following code works,but how to put another coordinate into "g"?or how to put many coordinate in a dataframe, and mapdist can read them ? and, can mapdist read in Chinese locations? Thanks for your reply!

g=c(121.754252477584,24.7528993303431)
c=c(121.752751736839,24.7554214120371)
mapdist(g,c,mode=c("driving","walking","bicycling"),output=c("simple"))
like image 764
user3751732 Avatar asked Mar 20 '23 07:03

user3751732


1 Answers

It's been a while since this was asked, but I thought I would add this for the help of future folks.

This command I learned from a script on Github – initially committed by Peter Schmiedeskamp – which alerted me to the fact that R was capable of grabbing drive-times from the Google Maps API. I used in an example on my blog to calculate drive times from various house sales to a city-center location.

location is the column containing each observation's long/lat coordinates, in the following format (-76.218922,36.841287). locMall is a column in my data set with the long/lat coords of the mall in each row. Just to clarify: each cell in this column had the exact same value, while each cell of location was different. Also something useful: mode can either be driving, walking, or bicycling.

library(ggmap)

library(plyr)

google_results <- rbind.fill(apply(subset(sample, select=c("location", "locMall")), 1, function(x) mapdist(x[1], x[2], mode="driving")))

Now let’s look at the results:

head(google_results,4)

          from                      to             m    km      miles   sec. minutes
1 (-76.219024, 36.901373) (-76.288018, 36.848950) 10954 10.954  6.806816 986 16.433333
2 (-76.243859, 36.868871) (-76.288018, 36.848950)  7279  7.279  4.523171 662 11.033333
3 (-76.296122, 36.859805) (-76.288018, 36.848950)  2101  2.101  1.305561 301  5.016667
4 (-76.264474, 36.938692) (-76.288018, 36.848950) 12844 12.844  7.981262 934 15.566667
    hours
1 0.27388889
2 0.18388889
3 0.08361111
4 0.25944444

*Edit: update. code now requires long, lat.

like image 99
CatfishSushi Avatar answered Apr 07 '23 10:04

CatfishSushi