jQuery从cookie中读取国家并放入Google变量

jQuery read country from cookie and put in Google variable

本文关键字:Google 变量 国家 cookie 读取 jQuery      更新时间:2023-09-26

我的谷歌地图脚本有一个困难的时间。它被设计为读取存储在cookie中的用户的国家,然后将其放入谷歌地图的自动建议变量中,因此它只建议来自他们国家的地方。

这是我一直在使用的代码:

  autocomplete = new google.maps.places.Autocomplete(input[0], {
    types: ["geocode"],
    componentRestrictions: {
      country: "uk"
    }
  });

然后我创建了这个:

// SET COOKIE FOR TESTING   
$.cookie("country", "us");
var country_code = $.cookie('country');
if (country_code) { options.componentRestrictions= { 'country': country_code }; }
var autocomplete = new google.maps.places.Autocomplete(input, options);

我似乎不知道如何将上面的代码实现到我的代码中,而不会破坏或读取它:http://jsfiddle.net/spadez/5dqHg/8/

TL;博士

我如何使它,所以我的当前代码读取cookie,并把它在"国家组件限制"变量谷歌地图自动建议?

尝试创建一个options对象并使用在cookie中找到的内容更新它:

var autocompleteOptions = {
    types: ["geocode"],
    componentRestrictions: {
        country: "uk"
    }
};
var country_code = $.cookie('country');
if (country_code) { 
    autocompleteOptions.componentRestrictions.country = country_code; 
}
var autocomplete = new google.maps.places.Autocomplete(input, autocompleteOptions);

已更新的小提琴:你应该注意,你的链接为jQuery。cookie插件在jsFiddle中无效。GitHub没有提供正确的mime类型,所以文件不会被解释为javascript