Retrieve Latitude & Longitude for an Address from Google Geocoder V3

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import sys
import pycurl, urllib
import simplejson as json

class GoogleLatLng:
    """
    Send an address to Google Geocoder API and get JSON output back.
    Parse to retrieve latitude and longitude.
    There is a 24-hour usage limit, currently this is 2500 requests 
    but this could change in the future. Check Google's Terms of Use
    before employing this technique.
    """
    def __init__(self):
        self.lat = ""
        self.lng = ""
        self.results = ""

    def parseResults(self, buff):
        self.results = json.loads(buff)
        try:
                self.lat = self.results['results'][0]['geometry']['location']['lat']
                self.lng = self.results['results'][0]['geometry']['location']['lng']
        except:
                print >> sys.stderr, "An error occurred.\nQuery results: %s" % self.results

    def requestLatLngJSON(self, location):
        location = urllib.quote_plus(location)
        c = pycurl.Curl()
        c.setopt(c.URL, 'http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=%s' % location)
        c.setopt(c.WRITEFUNCTION, self.parseResults)
        c.perform()
        c.close()

More like this

  1. Google Geocode Lookup by tonyskyday 5 years, 11 months ago
  2. Google v3 geocoding for Geodjango admin site by samhag 6 months, 2 weeks ago
  3. widget to capture a geographic Point by jerojasro 5 years, 2 months ago
  4. Get Lat Lng From Google Maps by dodolboks 2 years, 1 month ago
  5. Get Latitude and Longitude from google maps by b23 5 years, 5 months ago

Comments

(Forgotten your password?)