diff bandmap/static/js/bandmap.js @ 821:71db8076dc3d

Bandmap WIP: geocoding integrated with add form. Add form works. Before submitting the form, client side JS makes a geocode request to Google and populates hidden lat/lon fields with the result. Successfully created a model instance on the server side. Still need to update admin dashboard, admin approval, and give out badges for adding bands to the map. Once that is done, then work on displaying the map with filtering.
author Brian Neal <bgneal@gmail.com>
date Tue, 23 Sep 2014 20:40:31 -0500
parents 9a0df7bd2409
children 5103edd3acc4
line wrap: on
line diff
--- a/bandmap/static/js/bandmap.js	Sun Sep 21 18:20:29 2014 -0500
+++ b/bandmap/static/js/bandmap.js	Tue Sep 23 20:40:31 2014 -0500
@@ -1,3 +1,31 @@
+var bandmap = null;
+var geocoder = null;
+
+function addBandOnSubmit(event) {
+   var location = $('#id_location').val();
+   if (!location) {
+      alert("Please enter a location");
+      return false;
+   }
+   var button = $(this);
+   button.attr('disabled', 'disabled');
+   var form = $('#bandmap-add-form');
+   geocoder.geocode({'address': location}, function(results, status) {
+      button.removeAttr('disabled');
+      if (status == google.maps.GeocoderStatus.OK) {
+         $('#id_lat').val(results[0].geometry.location.lat().toString());
+         $('#id_lon').val(results[0].geometry.location.lng().toString());
+         $(form).submit();
+      }
+      else {
+         var msg = "Geocode unsuccessful: " + status + "\n" +
+            "Enter a new location";
+         alert(msg);
+      }
+   });
+   return false;
+}
+
 $(document).ready(function() {
    var map_div = $('#map-canvas');
    if (map_div.length) {
@@ -5,10 +33,12 @@
          center: {lat: 15.0, lng: -30.0},
          zoom: 2
       };
-      var map = new google.maps.Map(map_div[0], map_options);
+      bandmap = new google.maps.Map(map_div[0], map_options);
    }
 
    var add_form = $('#bandmap-add-form');
    if (add_form.length) {
+      geocoder = new google.maps.Geocoder();
+      $('#bandmap-add-submit').click(addBandOnSubmit);
    }
 });