Google v3 geocoding for Geodjango admin site

 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 1 year, 5 months ago
  2. GeoDjango maps in admin TabularInlines by alanB 3 years, 6 months ago
  3. GoogleAdmin: GMaps base layer in Geographic Admin (GeoDjango) by jbronn 5 years, 6 months ago
  4. Google Geocode Lookup by tonyskyday 6 years, 10 months ago
  5. Collapsed stacked inlines by Aneon 4 years, 11 months ago

Comments

(Forgotten your password?)