view 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 source
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) {
      var map_options = {
         center: {lat: 15.0, lng: -30.0},
         zoom: 2
      };
      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);
   }
});