Reputation: 12487
I've been playing around with the code and I have got something which nearly works but the coordinates don't actually get moved to my input boxes. I have rechecked the code a few times and the console doesn't show an errors so I am a little bit stuck.
Here is my code running:fiddle
Here is my 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 FOR 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 this 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 anyone could point me in the right direction I would be really grateful
Upvotes: 0
Views: 361
Reputation: 117334
the selector #searchform
doesn't match your form, the form doesn't have the id-attribute set to .
Use either the selector form[name="searchform"]
or set the id of the form to searchform
jquery.cookie.js
is served with content-type "text/plain"
and therefore(in some browsers) will be ignored, what results in an error because of undefined $.cookie
Upvotes: 2