【问题标题】:undefined geo location未定义的地理位置
【发布时间】:2012-10-22 07:12:31
【问题描述】:

我正在尝试使用 google 的 jsapi 获取客户端的当前位置,但我将 'google.loader' 视为未定义。知道为什么会发生这种情况吗?下面给出的是我使用的代码 sn-p。

$.getScript('http://www.google.com/jsapi', function() 
{
if(google.loader.ClientLocation) {
var visitor_lat = google.loader.ClientLocation.latitude;
var visitor_lon = google.loader.ClientLocation.longitude;
var visitor_city = google.loader.ClientLocation.address.city;
var visitor_region = google.loader.ClientLocation.address.region;
var visitor_country = google.loader.ClientLocation.address.country;
var visitor_countrycode = google.loader.ClientLocation.address.country_code;
console.log("Your location is: " + visitor_country);
}});

【问题讨论】:

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


    【解决方案1】:

    您只加载http://www.google.com/jsapi 这是基本的google 库,您可以使用该库加载实际的谷歌模块,如“ClientLocation”。

    使用类似的东西:

    google.load("my_library", function () {
        google.my_library; // is not available
    }
    

    my_library 更改为您需要的任何内容。

    【讨论】:

    • 如果您查看源代码,ClientLocation 已经填充了实际数据。
    • 未知,脚本创建window['google']['loader'],自己看:google.com/jsapi
    • 但是,为什么google.loader undefined?那时应该已经定义了。
    • 您是如何编辑上面给出结果的代码 sn-p 的?它似乎对我不起作用。
    • @TheZ 当我运行它时,我得到一个“Uncaught TypeError: Cannot read property 'latitude' of null”
    【解决方案2】:

    我今天也遇到了同样的问题。

    如果您使用的是手机浏览器,并且如果您禁用了浏览器的本地化,则 api 无法正确加载并引发 js 错误(解析错误)。

    我用iphone和android手机测试过。

    打开浏览器的本地化,就可以正常工作了!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-07
      • 1970-01-01
      • 1970-01-01
      • 2021-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多