This is my code: http://jsfiddle.net/E8sNt/1/
I was wondering for the following function:
if (coded === false) {
processLocation();
}
How I can get this to execute ONLY if the #loc input field actually has something in it. In sudo code it would be a bit like this, but I can't work out the proper code:
if (coded === false && #loc.val!=0) {
processLocation();
}
This is my full 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?
}
if (coded === false && $("#loc").val() != "") {
processLocation();
}
http://jsfiddle.net/E8sNt/2/
Or -
if(!coded && $('#loc').val()){
processLocation();
}
if (coded === false && $('#loc').val() ) {
processLocation();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With