How to use Google Maps Directions API to get zipcode and addresses passed through in python or R. -
recently, have been experimenting google maps api python , r. have gotten in python return list of directions point point b.
from googlemaps import googlemaps mapservice = googlemaps() directions = mapservice.directions('houston', 'atlanta') step in directions['directions']['routes'][0]['steps']: print step['descriptionhtml'] this returns list of turns etc, want extrapolate geocoordinates @ each step in directions. example, running 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 ramp on left <b>dallas</b> take exit <b>48a</b> <b>interstate 10 e</b> toward <b>beaumont</b> merge onto <b>i-10 e</b> keep left stay on <b>i-10 e</b> keep left stay on <b>i-10 e</b><div class="google_note">entering louisiana</div> keep left continue on <b>i-12 e</b>, follow signs <b>hammond</b> take exit <b>85b-85c</b> on left toward <b>i-59 n/<wbr/>i-10 e/<wbr/>bay st louis/<wbr/>hattiesburg</b> take exit <b>85c</b> on left 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 left merge onto <b>i-65 n</b> toward <b>montgomery</b> take exit <b>171</b> merge onto <b>i-85 n</b> toward <b>atlanta</b><div class="google_note">entering georgia</div> take exit <b>246</b> <b>fulton st/<wbr/>central ave</b> toward <b>downtown</b> keep right @ fork, follow signs <b>fulton street</b> turn right onto <b>fulton st sw</b> turn left onto <b>capitol ave se</b> how can i, example, current geocode location after step 1, step 2 etc. planning use information in r after, if easier in r better.
the documentation - http://py-googlemaps.sourceforge.net/ - tells how data direction steps. example:
>>> 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 package says uses deprecated google maps api (v2). i'm surprised works without api key, maybe tenth time try fail or something...
to in r, ggmap package , use route:
r = route("houston","atlanta",output="all") then structure bit complex there's documentation, can coordinates 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)
Comments
Post a Comment