0

Recently, I have been experimenting with the Google Maps API for python and R. I have gotten it in Python so it will return a list of directions to get from point A to point B.

from googlemaps import GoogleMaps

mapService = GoogleMaps()
directions = mapService.directions('Houston', 'Atlanta')
for step in directions['Directions']['Routes'][0]['Steps']:
    print step['descriptionHtml']

This returns a list of turns etc, but I want to extrapolate the geocoordinates at each step in the directions. For example, running the previous code returns:

Head <b>northeast</b> on <b>Bagby St</b> toward <b>Walker St</b>
Turn left onto <b>Walker St</b>
Merge onto <b>I-45 N</b> via the ramp on the left to <b>Dallas</b>
Take exit <b>48A</b> for <b>Interstate 10 E</b> toward <b>Beaumont</b>
Merge onto <b>I-10 E</b>
Keep left to stay on <b>I-10 E</b>
Keep left to stay on <b>I-10 E</b><div class="google_note">Entering Louisiana</div>
Keep left to continue on <b>I-12 E</b>, follow signs for <b>Hammond</b>
Take exit <b>85B-85C</b> on the left toward <b>I-59 N/<wbr/>I-10 E/<wbr/>Bay St Louis/<wbr/>Hattiesburg</b>
Take exit <b>85C</b> on the left to merge onto <b>I-10 E</b> toward <b>Bay St Louis</b><div class="google_note">Passing through Mississippi</div><div class="google_note">Entering Alabama</div>
Take exit <b>20</b> on the left to merge onto <b>I-65 N</b> toward <b>Montgomery</b>
Take exit <b>171</b> to merge onto <b>I-85 N</b> toward <b>Atlanta</b><div class="google_note">Entering Georgia</div>
Take exit <b>246</b> for <b>Fulton St/<wbr/>Central Ave</b> toward <b>Downtown</b>
Keep right at the fork, follow signs for <b>Fulton Street</b>
Turn right onto <b>Fulton St SW</b>
Turn left onto <b>Capitol Ave SE</b>

How can I, for example, get the current geocode location after step 1, step 2 etc. I am planning to use this information in R after, so if it is easier to do this in R that would be even better.

  • What do you mean by "geocode location"? Do you just mean latitude and longitude coordinate? Or something like a text description of the location? – Spacedman Aug 19 '14 at 06:58

1 Answers1

1

The documentation - http://py-googlemaps.sourceforge.net/ - tells you how to get data from the direction steps. For your example:

>>> for step in directions['Directions']['Routes'][0]['Steps']:
...   print step['Point']['coordinates'][1], step['Point']['coordinates'][0] 
... 
29.760427 -95.369803
29.76079 -95.36947
29.761142 -95.370061
29.766886 -95.366375
29.767338 -95.361398
29.776341 -95.269098

Note that package also says it uses a deprecated Google Maps API (v2). I'm also surprised this works without an API key, but maybe the tenth time I try it will fail or something...

To do in R, get the ggmap package and use route:

r = route("Houston","Atlanta",output="all")

then the structure is a bit complex but there's probably documentation, but you can get the coordinates for this route:

sapply(r$routes[[1]]$legs[[1]]$steps,function(s){c(s$start_location)})
    [,1]     [,2]      [,3]      [,4]      [,5]     [,6]     [,7]     
lat 29.76043 29.76079  29.76114  29.76689  29.76734 29.77634 30.08729 
lng -95.3698 -95.36947 -95.37006 -95.36638 -95.3614 -95.2691 -94.13588
    [,8]      [,9]      [,10]     [,11]     [,12]     [,13]     [,14]    
lat 30.41922  30.30775  30.30571  30.62566  32.36334  33.7326   33.73572 
lng -91.12063 -89.74687 -89.73971 -88.12116 -86.32169 -84.39195 -84.39144
    [,15]     [,16]    
lat 33.74191  33.74189 
lng -84.39116 -84.38779

Full example in help(route)

Spacedman
  • 92,590
  • 12
  • 140
  • 224