Geocoding with Google Maps


/ Published in: Python
Save to your folder(s)



Copy this code and paste it in your HTML
  1. # gLatLong.py - M.Keranen ([email protected]) - 01/09/2006
  2. # ------------------------------------------------------
  3. # Get lat/long coordinates of an address from Google Maps
  4.  
  5. import os,urllib
  6.  
  7. addr = raw_input('\nAddress or (Lat,Long): ')
  8. while addr <> '':
  9. url = ''
  10. if addr[0]=='(':
  11. center = addr.replace('(','').replace(')','')
  12. lat,lng = center.split(',')
  13. url = 'http://maps.google.com/maps?q=%s+%s' % (lat,lng)
  14. else:
  15. # Encode query string into URL
  16. url = 'http://maps.google.com/?q=' + urllib.quote(addr) + '&output=js'
  17. print '\nQuery: %s' % (url)
  18.  
  19. # Get XML location
  20. xml = urllib.urlopen(url).read()
  21.  
  22. if '<error>' in xml:
  23. print '\nGoogle cannot interpret the address.'
  24. else:
  25. # Strip lat/long coordinates from XML
  26. lat,lng = 0.0,0.0
  27. center = xml[xml.find('{center')+10:xml.find('}',xml.find('{center'))]
  28. center = center.replace('lat:','').replace('lng:','')
  29. lat,lng = center.split(',')
  30. url = 'http://maps.google.com/maps?q=%s+%s' % (lat,lng)
  31.  
  32. if url<>'':
  33. print 'Map: %s' % (url)
  34. os.startfile(url)
  35.  
  36. addr = raw_input('\nAddress or (Lat,Long): ')

URL: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/498128

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.