javascript - Jquery Logic - Can't efficiently achieve flag check -
i new javascript having hard time creating code can achieve logic:
i trying achieve following logic:
if user enters text in location box , click autosuggestion geocode , set coded flag "true"
if user enters text in location not click autosuggestion keep coded flag "false"
if user changes text in location box make sure "coded" flag set "false" - since text doesnt match coordinates fetched now
if user clicks search button check "coded" flag if true, submit form if false, process geocode, , if successful, submit form
this how far got before got stuck: link
even when click on autosuggested link, initiates geocode, , not change content in input box, when hit search says "location not geocoded - process location first". feel logic in programming wrong not sure how fix without lots of ugly repetitive code.
full code
geocode(); // set cookie testing purposes $.cookie("country", "uk"); // geocode function function geocode() { var coded = false; console.log(coded); var input = document.getelementbyid('loc'); var options = { types: ['geocode'] }; var country_code = $.cookie('country'); console.log(country_code); 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(); }); // on submit - work out if have results autocomplete function $('#searchform').on('submit', function(e) { console.log(coded); e.preventdefault(); if(coded == true) { console.log("location geocoded - submitting form"); $('#searchform').submit(); } else { console.log("location not geocoded - process location first"); processlocation(); } }); // check see if input has changed since being geocoded // if "coded" var false geocode when search button hit $("#loc").bind("change paste keyup", function() { var coded = false; console.log("content changed - coordinates no longer valid"); }); }; // geocode location function processlocation(){ var geocoder = new google.maps.geocoder(); var address = document.getelementbyid('loc').value; geocoder.geocode({ 'address': address }, // results - store coordinates in fields or error if not successful function(results, status) { if (status == google.maps.geocoderstatus.ok) { var coded = true; console.log("geocode successful"); $('#lat').val(results[0].geometry.location.lat()); $('#lng').val(results[0].geometry.location.lng()); } else { var coded = false; console.log("geocode unsuccessful"); alert("sorry - couldn't find location. please try alternative") } }); }
you using many local variables coded. should define 1 global flag , can use set it.
when so
function geocode() { .... var coded = false; ..... } function processlocation(){ .... var coded = true; .... } these not same coded. access globally. somethink
var coded = false; //initialize proper default value function geocode() { function geocode() { .... coded = false; ..... } function processlocation(){ .... coded = true; .... } note i'm putting coded outside of functions , access without var. should read on topic .
Comments
Post a Comment