This is my code: http://jsfiddle.net/E8sNt/1/
I was wondering about the following function:
if (coded === false) { processLocation(); }
How can I get this to execute ONLY if the #loc input field really has something in it. In sudo code, this will be a bit like this, but I cannot develop the correct code:
if (coded === false &&
This is my complete code:
var coded = false; geocode(); $.cookie("country", "uk"); // GEOCODE FUNCTION function geocode() { var input = $('#loc')[0]; var options = {types: ['geocode']}; var country_code = $.cookie('country'); if (country_code) { options.componentRestrictions = { 'country': country_code }; } var autocomplete = new google.maps.places.Autocomplete(input, options); google.maps.event.addListener(autocomplete, 'place_changed', function() { processLocation(); }); $('#searchform').on('submit', function(e) { if (coded === false) { processLocation(); } return true; }); $("#loc").bind("change paste keyup", function() { coded = false; }); } function processLocation() { var geocoder = new google.maps.Geocoder(); var address = $('#loc').val(); geocoder.geocode({ 'address': address }, function(results, status) { if (status === google.maps.GeocoderStatus.OK) { coded = true; $('#lat').val(results[0].geometry.location.lat()); $('#lng').val(results[0].geometry.location.lng()); } else { coded = false; alert("Sorry - We couldn't find this location. Please try an alternative"); } }); // coded = true; // Do we need this? }
Jimmy source share