The rules for this script are simple:
- If geocode is hit, then just geocode
- If geocode and submit is hit, then geocode and then submit
- If an autosuggest link is hit, then geocode instantly
- In this case, do not re-geocode later when the button is hit
This is my code, based on help and advice from here:
http://jsfiddle.net/spadez/Jfdbz/14/
$(function () {
var lastQuery = null,
lastResult = null, // new!
autocomplete,
processLocation = function (callback) { // accept a callback argument
var input = $("#loc"),
lat = $("#lat"),
long = $("#lng");
var query = $.trim(input.val()),
geocoder;
// if query is empty or the same as last time...
if (!query || query === lastQuery) {
if (callback) {
callback(lastResult); // send the same result as before
}
return; // and stop here
}
lastQuery = query; // store for next time
geocoder = new google.maps.Geocoder();
geocoder.geocode({
address: query
}, function (results, status) {
if (status === google.maps.GeocoderStatus.OK) {
lat.val(results[0].geometry.location.lat());
long.val(results[0].geometry.location.lng());
lastResult = true; // success!
} else {
alert("Sorry - We couldn't find this location. Please try an alternative");
lastResult = false; // failure!
}
if (callback) {
callback(lastResult); // send the result back
}
});
},
ctryiso = $("#ctry").val(),
options = {
types: ["geocode"]
};
if (ctryiso !== '') {
options.componentRestrictions = {
'country': ctryiso
};
}
autocomplete = new google.maps.places.Autocomplete($("#loc")[0], options);
google.maps.event.addListener(autocomplete, 'place_changed', processLocation);
$('#search').click(function (e) {
var form = $(this).closest('form');
e.preventDefault(); // stop the submission
processLocation(function (success) {
if (success) { // if the geocoding succeeded, submit the form
form.submit();
}
});
});
$('#geosearch').click(function (e) {
var form = $(this).closest('form');
e.preventDefault(); // stop the submission
processLocation();
});
});
I think I have reached the point where it works correctly, and I can't seem to make it any more structured, any more readable, any more efficient or compact. However, I am new to javascript and I would be really open to hear if anyone feels that they can improve on this.
I must admit I a bit pedantic and get a lot of satisfaction out of the refining process!
EDIT: I pasted the code wrong - fixed