Created
December 1, 2010 22:25
-
-
Save alexwelch/724348 to your computer and use it in GitHub Desktop.
suggests your city and state based on the zip code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// requires google maps api: "http://maps.google.com/maps/api/js?sensor=false" | |
// and of course jQuery | |
(function($){ | |
$.locationSuggest = function(el, options){ | |
var base = this; | |
base.$el = $(el); | |
base.el = el; | |
base.$el.data("locationSuggest", base); | |
base.init = function() { | |
base.options = $.extend({},$.locationSuggest.defaultOptions, options); | |
base.generateJQuerySelectors(); | |
base.bindZipListener(); | |
}; | |
base.generateJQuerySelectors = function() { | |
// this goes through all the selectors and assigns them to their $ objects, so: | |
// options.selectors.options_panel has a base.$options_panel equivelent. | |
for (var key in base.options.selectors) { | |
base['$' + key] = $(base.options.selectors[key]); | |
} | |
}; | |
base.bindZipListener = function() { | |
base.$el.change(function(e) { | |
var zip = $(e.target).attr('value'); | |
base.getLocation(zip); | |
// if (zip.length === 5) { | |
// base.getLocation(zip); | |
// } | |
}); | |
}; | |
base.displayResults = function(results) { | |
var city_state = [base.getCity(results), base.getState(results)].join(', ') | |
var result_container = 'span.location_results'; | |
$(result_container).remove(); | |
var $html = $("<span class='location_results'>" + city_state + "<a href='#wrong_city'>Not your city?</a></span>"); | |
base.$el.after($html); | |
$(result_container).find('a').click(function(e) { | |
base.$hidden_fields.removeClass('hidden'); | |
e.preventDefault(); | |
}); | |
}; | |
base.populateLocations = function(results) { | |
base.$city_input.val(base.getCity(results)); | |
var state = base.getState(results); | |
var select_vals = $.map(base.$state_input.find('option'), function(el) { return $(el).val(); }); | |
if ($.inArray(state, select_vals) != -1) { | |
base.$state_input.val(state) | |
} | |
}; | |
base.getLocation = function(zip) { | |
var geocoder = new google.maps.Geocoder(); | |
if (geocoder) { | |
geocoder.geocode({ 'address': zip }, function (results, status) { | |
if (status === google.maps.GeocoderStatus.OK) { | |
base.displayResults(results); | |
base.populateLocations(results); | |
} else { | |
log("Geocoding failed: " + status); | |
} | |
}); | |
} | |
}; | |
base.getCity = function(results) { | |
return base.getAddressComponent(results, 'locality'); | |
}; | |
base.getState = function(results) { | |
return base.getAddressComponent(results, 'administrative_area_level_1'); | |
}; | |
base.getAddressComponent = function(results, type) { | |
var results_with_type = $.grep(results[0].address_components, function(v, i) { | |
return (v.types[0] === type); | |
}); | |
try { | |
return results_with_type[0].short_name; | |
} catch (err) { | |
return type + ' unavailable'; | |
} | |
}; | |
base.init(); | |
}; | |
$.locationSuggest.defaultOptions = { | |
selectors: { | |
hidden_fields: '.city_and_state', | |
city_input: '#schedule_test_drive_city', | |
state_input: '#schedule_test_drive_region' | |
} | |
}; | |
$.fn.locationSuggest = function(options){ | |
return this.each(function(){ | |
(new $.locationSuggest(this, options)); | |
}); | |
}; | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment