Geocode
Geocode lookup formats using Nominatim queries.
- geocode
Geocode lookup formats using Nominatim queries.
This only works with Point geometry. [video](http://www.youtube.com/watch?v=gZ7_n177sTE&list=HL1351725584&feature=mh_lolz) Rename the snippet as gmgdav3.js and save it to template/admin with [gmgdav3.html](http://djangosnippets.org/snippets/2840/) * - *models.py*: ` from django.contrib.gis.db import models` ` class point(models.Model):` ` address = models.CharField(max_length=100, help_text='Press "Tab" to refresh the map')` ` longitude = models.FloatField(help_text='WGS84 Decimal Degree. Press "Tab" to refresh the map')` ` latitude = models.FloatField(help_text='WGS84 Decimal Degree. Press "Tab" to refresh the map')` ` in_geom = models.PointField('shp', srid=4326)` ` objects = models.GeoManager()` ` def __unicode__(self):` ` return str(self.address)` * - *admin.py*: ` from models import * ` ` from django.conf import settings` ` from django.contrib.gis import admin` ` from django.contrib.gis.geos import GEOSGeometry` ` class GoogleAdmin(admin.OSMGeoAdmin):` ` g = GEOSGeometry('POINT (9.191884 45.464254)') # Set map center` ` g.set_srid(4326)` ` g.transform(900913)` ` default_lon = int(g.x)` ` default_lat = int(g.y)` ` default_zoom = 7` ` extra_js = ["http://maps.google.com/maps/api/js?v=3.2&sensor=false"]` ` map_template = 'gmgdav3.html'` ` admin.site.register(point, GoogleAdmin)` ` # admin.site.register(your other models...,...)`
Very simple python class for querying Google Geocoder. Accepts a human-readable address, parses JSON results and sets lat and lng. (Full JSON results are stored to `results` property of GoogleLatLng for access to other attributes.) This could easily be expanded to include the XML output option, etc. **Requires PycURL and simplejson.** Example: >>> location = "1600 Amphitheatre Parkway, Mountain View, CA 94043" >>> glatlng = GoogleLatLng() >>> glatlng.requestLatLngJSON(location) >>> print "Latitude: %s, Longitude: %s" % (glatlng.lat, glatlng.lng) Latitude: 37.422782, Longitude: -122.085099` ** Do not forget the usage limits, which include request rate throttling, otherwise, Google might ban you. ** ===
Makes a call to Google's geocoder and returns the latitude and longitude as a string or returns an empty string. Called by the save method to save the lat and long to the db to be used when rendering maps on the frontend. Reduces the number of calls to geocoder by calling only when saving, not on every viewing of the object. Be sure to import *urllib* and the project's *settings*, and to define GOOGLE_API_KEY in settings.py. **Example:** def save(self): location = "%s+%s+%s+%s" % (self.address, self.city, self.state, self.zip_code) self.lat_long = get_lat_long(location) if not self.lat_long: location = "%s+%s+%s" % (self.city, self.state, self.zip_code) self.lat_long = get_lat_long(location) super(Foo, self).save()
4 snippets posted so far.