javascript - Google Geocoding - Coordinates not being returned into input box -
i've been playing around code , have got works coordinates don't moved input boxes. have rechecked code few times , console doesn't show errors little bit stuck.
here code running:fiddle
here actual code:
<script type="text/javascript" src="//maps.google.com/maps/api/js?sensor=true&libraries=places"></script> <script type="text/javascript" src="https://raw.github.com/carhartl/jquery-cookie/master/jquery.cookie.js"></script> <script type="text/javascript"> // set cookie testing $.cookie("country", "uk"); // geocode result function geocode(){ var geocoded = { done: false }; var input = document.getelementbyid('loc'); var options = { types: ['geocode']}; var country_code = $.cookie('country'); alert(country_code); if (country_code) { options.componentrestrictions= { 'country': country_code }; } var autocomplete = new google.maps.places.autocomplete(input, options); $('#searchform').on('submit',function(e){ if(geocoded.done) return true; e.preventdefault(); var geocoder = new google.maps.geocoder(); var address = document.getelementbyid('loc').value; $('#searchform input[type="submit"]').attr('disabled',true); geocoder.geocode({ 'address': address }, function (results, status) { if (status == google.maps.geocoderstatus.ok) { $('#lat').val(results[0].geometry.location.lat()); $('#lng').val(results[0].geometry.location.lng()); geocoded.done = true; alert("geocoded!"); $('#searchform').submit(); } else { $('#searchform input[type="submit"]').attr('disabled',false); alert("we couldn't find location") } }); }); }; </script> <body onload="geocode()"> <form name="searchform"> <input class="kw" id="keyword" placeholder="keyword"></input> <input id="loc" placeholder="location" type="text"></input> <input type="submit" value="search" id="search"> <input class="hidden" id="lat" disabled="true" placeholder="lat"></input> <input class="hidden" id="lng" disabled="true" placeholder="lng"></input> </form>
if point me in right direction grateful
the selector
#searchform
doesn't match form, form doesn't have id-attribute set .use either selector
form[name="searchform"]
or set id of formsearchform
- githhub not cdn,
jquery.cookie.js
served content-type"text/plain"
, therefore(in browsers) ignored, results in error because of undefined$.cookie
Comments
Post a Comment