We Recommend

Learning Python Learning Python
The authors of Learning Python show you enough essentials of the Python scripting language to enable you to begin solving problems right away, then reveal more powerful aspects of the language one at a time. This approach is sure to appeal to programmers and system administrators who have urgent problems and a preference for learning by semi-guided experimentation.


Posted By

sudarkoff on 09/27/06


Tagged

location google map api


Versions (?)


Who likes this?

3 people have marked this snippet as a favorite

anayhk
eunjoo1984
neuroasis


Geocoding with Google Maps


Published in: Python 


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

  1. # gLatLong.py - M.Keranen (mksql@yahoo.com) - 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): ')

Report this snippet 

You need to login to post a comment.