Atheuz

Untitled

Apr 10th, 2012 (edited)
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.30 KB | None | 0 0
  1. # -*- coding: utf8 -*-
  2.  
  3. # Filename:     google_api_test.py
  4. # Desc:         https://developers.google.com/maps/documentation/directions/#RequestParameters
  5. # First edited: 10-04-2012 12:40
  6. # Last edited:  10-04-2012 13:11
  7.  
  8. import urllib
  9. import urllib2
  10. import lxml.etree
  11.  
  12. # "http://maps.googleapis.com/maps/api/directions/xml?origin=miami&destination=atlanta&sensor=false"
  13.  
  14. # "http://maps.googleapis.com/maps/api/directions/json?origin=Aalborg&destination=Aarhus&sensor=false"
  15.  
  16. def read_xml(s):
  17.     return lxml.etree.fromstring(s)
  18.  
  19. def directions(origin="", destination=""):
  20.     if len(origin) > 1 and len(destination) > 1:
  21.         urldata = {"origin":      origin,
  22.                    "destination": destination,
  23.                    "language":    "da-DK",
  24.                    "sensor":      "false"}
  25.         url = "http://maps.googleapis.com/maps/api/directions/xml"
  26.         data = urllib.urlencode(urldata)
  27.         url = url + "?" + data
  28.         content = urllib2.urlopen(url).read()
  29.         content = read_xml(content)
  30.         distances = content.xpath('//distance/value/text()')
  31.         distances = map(int, distances)
  32.         print distances[-1]
  33.  
  34. def main():
  35.     directions(origin="Vendelparken 30, Sulsted", destination="Badehusvej 13, Aalborg")
  36.    
  37.  
  38. if __name__ == '__main__':
  39.     main()
Add Comment
Please, Sign In to add comment