javascript - Jquery newbie - Geocoder not returning results -
this first javascript have tried put not having lot of luck. script should do:
geocode address either clicking on autosuggested location or clicking search button if not have result clicking autosuggestion. submit form.
i not having luck, seems have mucked braketing on script because no matter complains exceptions.
this code: link
geocode(); // set cookie testing purposes $.cookie("country", "us"); // geocode function function geocode() { var coded = false; var input = document.getelementbyid('loc'); 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(); }); // on submit - work out if have results autocomplete function $('#searchform').on('submit', function(e) { e.preventdefault(); if(coded = false;) { processlocation(); } else { $('#searchform').submit(); } }); // 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; }); }; // geocode location function processlocation(){ var geocoder = new google.maps.geocoder(); var address = document.getelementbyid('loc').value; $('#searchform input[type="submit"]').attr('disabled', true); 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; $('#lat').val(results[0].geometry.location.lat()); $('#lng').val(results[0].geometry.location.lng()); } else { var coded = false; $('#searchform input[type="submit"]').attr('disabled', false); alert("we couldn't find location") } }); } can see have gone wrong?
p.s because first script, if knowledge able tell me if have made poor choices in terms of design of script, appreciate it, want make first script cleanly coded possible.
there syntax error
if(coded = false;) { should
if(coded == false) { checking console have told such thing , place error occured.... here's fixed fiddle
Comments
Post a Comment