From 036d70ce984badaa24149459596457cb2ce814c0 Mon Sep 17 00:00:00 2001 From: Andrew Williams Date: Mon, 1 Apr 2013 17:17:31 +0100 Subject: [PATCH] Rework the Add Store to use clientside geocoding. --- app/stores/forms.py | 6 +++- app/stores/static/js/geocoder.js | 30 +++++++++++++++++++ .../templates/stores/wizard/store_wizard.html | 8 ++++- app/stores/views/search.py | 7 +++-- app/stores/views/stores.py | 2 +- 5 files changed, 48 insertions(+), 5 deletions(-) create mode 100644 app/stores/static/js/geocoder.js diff --git a/app/stores/forms.py b/app/stores/forms.py index 2513c52..2b32cb7 100644 --- a/app/stores/forms.py +++ b/app/stores/forms.py @@ -48,7 +48,11 @@ class AddressForm(BootstrapModelForm): class Meta: model = Address - exclude = ('name', 'geo_latitude', 'geo_longitude') + exclude = ('name',) + widgets = { + 'geo_latitude': forms.widgets.HiddenInput(), + 'geo_longitude': forms.widgets.HiddenInput(), + } class AddressInline(InlineFormSet): diff --git a/app/stores/static/js/geocoder.js b/app/stores/static/js/geocoder.js new file mode 100644 index 0000000..769d584 --- /dev/null +++ b/app/stores/static/js/geocoder.js @@ -0,0 +1,30 @@ + +function get_form_latlng() { + var addr = $('[id$=address1]').val() + ', ' + $('[id$=city]').val() + ', ' + $('[id$=postcode]').val(); + console.log('Inputted address: ' + addr); + var gc = new google.maps.Geocoder(); + gc.geocode({'address': addr },update_form_latlng); + return false +} + +function update_form_latlng(res, status) { + if (status == google.maps.GeocoderStatus.OK) { + if (res.length > 0) { + res = res[0]; + console.log('Found location: ' + res.formatted_address); + $('[id$=geo_latitude]').val(res.geometry.location.lat()); + $('[id$=geo_longitude]').val(res.geometry.location.lng()); + } + $('.form').submit(); + } else { + alert('Unable to lookup the location for the address provided. Please check and resubmit.'); + } +} + +$(document).ready(function(){ + if ($('[id$=geo_latitude]') && $('[id$=geo_latitude]').val() == "" ) { + $('.form').submit(function(){ + get_form_latlng(); + }) + } +}); diff --git a/app/stores/templates/stores/wizard/store_wizard.html b/app/stores/templates/stores/wizard/store_wizard.html index 8bf5ae5..0748a05 100644 --- a/app/stores/templates/stores/wizard/store_wizard.html +++ b/app/stores/templates/stores/wizard/store_wizard.html @@ -1,10 +1,16 @@ {% extends "base.html" %} {% load bootstrap %} +{% load staticfiles %} {% block style %} {{ form.media }} {% endblock %} +{% block scripts %} + + +{% endblock %} + {% block content %}