Я пытаюсь установить файл cookie, а затем прочитать его и поместить в свой скрипт геокодирования Google, но он, похоже, не работает. Это должно быть так, что единственные результаты аутосубпозиции взяты из США, но вы можете видеть в примере, что они предлагаются из каждой страны.JQuery Cookie не читается правильно
Это код, я работаю с:
http://jsfiddle.net/sR4GR/4/
Это исходный код:
<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", "us");
// GEOCODE RESULT
function geocode(){
var GeoCoded = { done: false };
var input = document.getElementById('loc');
var options = { types: []};
var country_code = $.cookie('country');
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;
$('#searchform').submit();
} else {
$('#searchform input[type="submit"]').attr('disabled',false);
alert("Damn! 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>
Когда я ввожу в поле ввода, автообновление выполняется только с США. Если я сменил cookie на ES, они из Испании. Кажется, он работает правильно. Пробовал в Chrome и Firefox. – rGil
Благодарим вас за ответ. Это странно, когда я запускаю этот код, он не работает, я могу набрать «Лондон» либо «es», либо «us», и он по-прежнему autosuggests меня иметь Лондон, Соединенное Королевство, которое он не должен делать – Jimmy
Если я установил этот строка "var country_code = $ .cookie ('страна'); к этому "var country_code =" us ", он работает, поэтому это должно быть ошибка чтения файла cookie – Jimmy