I'm a Python Web Dev noob, but I had a similar requirement. I created
this function to convert a postcode into lon & lat data suitable for
use in Google Maps. It's a work in progress, not very elegant, and it
doesn't have any error management but it's a start. Hopefully someone
on the list will be able to point out any serious problems with it if
there are any ;-)

    import urllib
    import xml.dom.minidom
    """Returns latitude and longitude when passed a postcode
    """
    geocodeUrl='http://maps.googleapis.com/maps/api/geocode/xml?address='
    sensor='&sensor=false'
    #send the postcode to Google for geocoding
    dom=xml.dom.minidom.parse(urllib.urlopen(geocodeUrl+address+sensor))

    #grab the location element
    location=dom.getElementsByTagName('location')[0]

    #pull out the lat & lng elements and remove the <lat> & <lng> tags
    
lat=location.getElementsByTagName('lat')[0].toxml().replace('<lat>','').replace('</lat>','')
    
lng=location.getElementsByTagName('lng')[0].toxml().replace('<lng>','').replace('</lng>','')

    georesults = {'lat':lat, 'lng':lng}

    return georesults

Reply via email to