Rework the Add Store to use clientside geocoding.

This commit is contained in:
2013-04-01 17:17:31 +01:00
parent ec6a2accc3
commit 036d70ce98
5 changed files with 48 additions and 5 deletions

View File

@@ -48,7 +48,11 @@ class AddressForm(BootstrapModelForm):
class Meta: class Meta:
model = Address model = Address
exclude = ('name', 'geo_latitude', 'geo_longitude') exclude = ('name',)
widgets = {
'geo_latitude': forms.widgets.HiddenInput(),
'geo_longitude': forms.widgets.HiddenInput(),
}
class AddressInline(InlineFormSet): class AddressInline(InlineFormSet):

View File

@@ -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();
})
}
});

View File

@@ -1,10 +1,16 @@
{% extends "base.html" %} {% extends "base.html" %}
{% load bootstrap %} {% load bootstrap %}
{% load staticfiles %}
{% block style %} {% block style %}
{{ form.media }} {{ form.media }}
{% endblock %} {% endblock %}
{% block scripts %}
<script type="text/javascript" src="https://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript" src="{% static "js/geocoder.js" %}"></script>
{% endblock %}
{% block content %} {% block content %}
<div class="page-header"> <div class="page-header">
<h1>Add Store ({{ wizard.steps.step1 }} of {{ wizard.steps.count }})</h1> <h1>Add Store ({{ wizard.steps.step1 }} of {{ wizard.steps.count }})</h1>
@@ -18,6 +24,6 @@
<button name="wizard_goto_step" class="btn" type="submit" value="{{ wizard.steps.first }}">First Step</button> <button name="wizard_goto_step" class="btn" type="submit" value="{{ wizard.steps.first }}">First Step</button>
<button name="wizard_goto_step" class="btn" type="submit" value="{{ wizard.steps.prev }}">Previous Step</button> <button name="wizard_goto_step" class="btn" type="submit" value="{{ wizard.steps.prev }}">Previous Step</button>
{% endif %} {% endif %}
<button name="wizard_goto_step" class="btn" type="submit" value="{{ wizard.steps.next }}">Next Step</button> <button class="btn" type="submit">Next Step</button>
</form> </form>
{% endblock %} {% endblock %}

View File

@@ -18,8 +18,9 @@ class DistanceSearchView(ListView):
if location: if location:
name, geo = caching_geo_lookup(location) name, geo = caching_geo_lookup(location)
elif lat and lng: elif lat and lng:
name, geo = caching_geo_lookup('%s,%s' % (lat, lng)) geo = (lat, lng)
print name else:
geo = None
self.location_geo = geo self.location_geo = geo
return Point(geo[1], geo[0]) return Point(geo[1], geo[0])
@@ -29,6 +30,8 @@ class DistanceSearchView(ListView):
def get_queryset(self): def get_queryset(self):
location = self.get_location() location = self.get_location()
if not location:
return SearchQuerySet.none
distance = self.get_distance() distance = self.get_distance()
print location, distance print location, distance
return SearchQuerySet().dwithin('location', location, distance).distance('location', location).order_by('-distance') return SearchQuerySet().dwithin('location', location, distance).distance('location', location).order_by('-distance')

View File

@@ -62,7 +62,7 @@ class StoreCreateView(SessionWizardView):
messages.success(self.request, "%s has been sumbitted for moderation and should be visible within the next 24 hours." % store_obj) messages.success(self.request, "%s has been sumbitted for moderation and should be visible within the next 24 hours." % store_obj)
return HttpResponseRedirect(reverse('store-map')) return HttpResponseRedirect(reverse('stores-map'))
def get_template_names(self): def get_template_names(self):
return 'stores/wizard/store_wizard.html' return 'stores/wizard/store_wizard.html'