【问题标题】:jQuery read country from cookie and put in Google variablejQuery 从 cookie 中读取国家并放入 Google 变量
【发布时间】:2013-06-10 12:00:53
【问题描述】:

我在使用 Google 地图脚本时遇到了困难。它旨在读取存储在 cookie 中的用户所在的国家/地区,然后将其放入 Google 地图的自动建议变量中,因此它仅建议来自其国家/地区的地点。

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

  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;DR

如何让我的current code 读取 cookie 并将其放入 Google 地图自动建议的“国家/地区组件限制”变量中?

【问题讨论】:

    标签: javascript jquery google-maps variables google-maps-api-3


    【解决方案1】:

    尝试创建一个 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);
    

    Updated Fiddle:您应该注意 jQuery.cookie 插件的链接在 jsFiddle 中无效。 GitHub 没有提供正确的 mime 类型,因此文件不会被解释为 javascript

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-25
      • 2019-07-20
      • 1970-01-01
      • 2021-11-28
      • 2022-12-31
      • 1970-01-01
      • 2016-06-11
      相关资源
      最近更新 更多