Login

Google v3 geocoding for Geodjango admin site

Author:
samhag
Posted:
October 31, 2012
Language:
JavaScript
Version:
1.3
Tags:
admin google-maps geocode geolocation geodjango
Score:
3 (after 3 ratings)

This only works with Point geometry. video

Rename the snippet as gmgdav3.js and save it to template/admin with gmgdav3.html

    • 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...,...)

 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
{% extends "gis/admin/openlayers.js" %}
{% block base_layer %}new OpenLayers.Layer.Google("Google Hybrid", {type: google.maps.MapTypeId.HYBRID});{% endblock %}

{% block controls %}
{{ block.super }}

django.jQuery(document).ready(function() {
	
	var mappa = {{ module }}.map;
	var lng, lat
	var $address = django.jQuery('#id_address');
	
	$address.change(function() {
		geocod($address.val(), mappa);
	});
	
	django.jQuery('#id_longitude, #id_latitude').change(function() {
		lng = django.jQuery("#id_longitude").val();
		lat = django.jQuery("#id_latitude").val();
		modcoo(lng, lat, mappa);
		revgeocod(lng, lat, mappa); 
	});
	
	django.jQuery('[id*="OpenLayers.Layer.Vector_39_"]').click(function() { 
		srco = document.getElementById('{{ id }}').value;
		var a = srco.split(" ");
		var b = a[0].split("(");
		var c = a[1].split(")");
		lngm = parseFloat(c[0]);
		latm = parseFloat(b[1]);
		var c = new OpenLayers.Geometry.Point(latm,lngm).transform(new OpenLayers.Projection("EPSG:900913"), new OpenLayers.Projection("EPSG:4326"));
		input_lng_lat(c.x,c.y, mappa);
		revgeocod(c.x, c.y, mappa);
	});
  
});

function modcoo(lng, lat, mappa) {
    mappa.setCenter(new OpenLayers.LonLat(lng,lat).transform(new OpenLayers.Projection("EPSG:4326"), new OpenLayers.Projection("EPSG:900913")), 13);
	var c = new OpenLayers.Geometry.Point(lng,lat).transform(new OpenLayers.Projection("EPSG:4326"), new OpenLayers.Projection("EPSG:900913"));
	{{ module }}.layers.vector.addFeatures([new OpenLayers.Feature.Vector(c)]);
}

function input_lng_lat(lng, lat, mappa) {
	django.jQuery("#id_longitude").val(lng.toFixed(6));
	django.jQuery("#id_latitude").val(lat.toFixed(6));
}

function geocod(ind, mappa) {
	var geocoder = new google.maps.Geocoder();
	geocoder.geocode({'address': ind} ,
        function(results,status) { 
			if (status == google.maps.GeocoderStatus.OK) {
				if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
					lat = results[0].geometry.location.lat();  
					lng = results[0].geometry.location.lng(); 
					mappa.setCenter(new OpenLayers.LonLat(lng,lat).transform(new OpenLayers.Projection("EPSG:4326"), new OpenLayers.Projection("EPSG:900913")), 13);
					var c = new OpenLayers.Geometry.Point(lng,lat).transform(new OpenLayers.Projection("EPSG:4326"), new OpenLayers.Projection("EPSG:900913"));
					{{ module }}.layers.vector.addFeatures([new OpenLayers.Feature.Vector(c)]);
					input_lng_lat(lng, lat, mappa);
				}	
			}
			else {
				alert("Address not found!");
			}
        }
	)  
};

function revgeocod(lng, lat, mappa) {
	var geocoder = new google.maps.Geocoder();
	var infowindow = new google.maps.InfoWindow();
	var latlng = new google.maps.LatLng(lat,lng);
    geocoder.geocode({'latLng': latlng}, function(results, status) {
		if (status == google.maps.GeocoderStatus.OK) {
			django.jQuery("#id_address").val(results[0].formatted_address);
		} else {
			alert("Geocoder failed due to: " + status);
		}
	});
};	
	
{% endblock %}

More like this

  1. template code for "Google v3 geocoding for Geodjango admin site" by samhag 2 years, 6 months ago
  2. GeoDjango maps in admin TabularInlines by alanB 4 years, 7 months ago
  3. GoogleAdmin: GMaps base layer in Geographic Admin (GeoDjango) by jbronn 6 years, 7 months ago
  4. Google Geocode Lookup by tonyskyday 7 years, 11 months ago
  5. Collapsed stacked inlines by Aneon 6 years ago

Comments

Please login first before commenting.