1 /**
  2  * Creates a module that listens to the given form for a new location, adds it
  3  * to the map, and pans/zooms the map.
  4  * @constructor
  5  * @param {GEPlugin} plugin An instance of GoogleEarthExtensions
  6  * @param {HTMLFormElement} location A form with a single text input for a location to geocode
  7  */
  8 madrona.map.geocoder = function(gex, form){
  9     // Will need to have google maps api v3 already loaded
 10     this.geocoder = new google.maps.Geocoder();
 11     this.form = form;
 12     this.get = ge;
 13     var self = this;
 14     
 15     var clearPlacemark = function(dont_clear_input){
 16         if(self.placemark){
 17             gex.dom.removeObject(self.placemark);
 18             self.placemark = false;
 19         }
 20         if(dont_clear_input !== true){
 21             $(self.form).find('input[name=flyto_location]').val('');
 22         }
 23         $(self.form).find('#flytoclear').addClass('disabled');
 24         return false;
 25     };
 26     
 27     var callback = function(e){
 28         var start_time = new Date().getTime();
 29         self.latest_geocode = start_time;
 30         clearPlacemark(true);
 31         var location = $(self.form).find('input:first').val();
 32         if(!location){
 33             alert('You need to type in a location name.');
 34             return false;
 35         }
 36         self.geocoder.geocode({ address: location, country: '.us'}, function(results, status){
 37             if(status == google.maps.GeocoderStatus.OK && results.length){
 38                 if(status != google.maps.GeocoderStatus.ZERO_RESULTS){
 39                     // Only proceed if this is the latest geocoder initiated
 40                     // If not, do nothing since it has been "orphaned" by a newer query 
 41                     if (start_time == self.latest_geocode) {
 42                         //provide appropriate map extent (bounding box) for each location
 43                         var viewport = results[0].geometry.viewport; 
 44                         var sw = new geo.Point(viewport.getSouthWest());
 45                         var ne = new geo.Point(viewport.getNorthEast());
 46                         var bounds = new geo.Bounds(sw, ne);
 47                         var opts = {aspectRatio: 1.0};
 48                         var bounding_view = gex.view.createBoundsView(bounds, opts);
 49                         ge.getView().setAbstractView(bounding_view);
 50                         
 51                         var point = results[0].geometry.location;
 52                         self.placemark = gex.dom.addPointPlacemark([point.lat(), point.lng()], {
 53                         stockIcon: 'shapes/cross-hairs',
 54                         name: location
 55                         });
 56                         $(self.form).find('#flytoclear').removeClass('disabled');
 57                     }
 58                 }else{
 59                     alert("geocoder didn't find any results.");
 60                 }
 61             }else{
 62                 alert("Geocoder failed due to: " + status);
 63             }
 64         });        
 65         e.preventDefault();
 66         return false;        
 67     }
 68     $(this.form).find('#flytogo').click(callback);
 69     $(this.form).find('#flytoclear').click(clearPlacemark);
 70     $(this.form).submit(callback);
 71     
 72 };
 73 
 74 /**
 75  * Prepare instance for destruction by remove event listeners.
 76  */
 77 madrona.map.geocoder.prototype.destroy = function(){
 78     $(this.form).find('#flytogo').unbind('click');
 79     $(this.form).unbind('submit');
 80 };
 81