Getting driving distance between two points (lat, lon) using R and Google Map API

前端 未结 5 594
失恋的感觉
失恋的感觉 2021-01-30 18:29

I am trying to get the driving distance between two points with lat/lon given. I can manually put them into google map and get the driving distance but I want to do all this pr

5条回答
  •  太阳男子
    2021-01-30 19:11

    I authored the gmapsdistance package to do just that. It is available on CRAN. You can use the function in the following way:

    results = gmapsdistance(origin = "38.1621328+24.0029257",
                            destination = "37.9908372+23.7383394",
                            mode = "walking") results
    # $Time
    # [1] 30025
    # 
    # $Distance
    # [1] 39507
    # 
    # $Status
    # [1] "OK"
    

    You can also include vectors of origins and destinations, and get the resulting distance matrix. It supports also directions, and has a bunch of options:

    results = gmapsdistance(origin = c("Washington+DC", "New+York+NY", "Seattle+WA", "Miami+FL"), 
                            destination = c("Los+Angeles+CA", "Austin+TX", "Chicago+IL", "Philadelphia+PA"), 
                            mode = "bicycling", 
                            departure = 1514742000)
    results
    # $Time
    #              or Time.Los+Angeles+CA Time.Austin+TX Time.Chicago+IL Time.Philadelphia+PA
    # 1 Washington+DC              856621         535146          247765                54430
    # 2   New+York+NY              917486         596011          308630                32215
    # 3    Seattle+WA              374692         678959          674989               956702
    # 4      Miami+FL              829039         416667          452035               411283
    # 
    # $Distance
    #              or Distance.Los+Angeles+CA Distance.Austin+TX Distance.Chicago+IL Distance.Philadelphia+PA
    # 1 Washington+DC                 4567470            2838519             1303067                   266508
    # 2   New+York+NY                 4855086            3126136             1590684                   160917
    # 3    Seattle+WA                 1982354            3562970             3588297                  5051951
    # 4      Miami+FL                 4559205            2279966             2381610                  2169382
    # 
    # $Status
    #              or status.Los+Angeles+CA status.Austin+TX status.Chicago+IL status.Philadelphia+PA
    # 1 Washington+DC                    OK               OK                OK                     OK
    # 2   New+York+NY                    OK               OK                OK                     OK
    # 3    Seattle+WA                    OK               OK                OK                     OK
    # 4      Miami+FL                    OK               OK                OK                     OK
    

提交回复
热议问题