【问题标题】:Google ClientLocation API issueGoogle ClientLocation API 问题
【发布时间】:2012-09-20 05:41:45
【问题描述】:

我正在使用 google API 来检测客户端位置,但它总是向我显示“Google Loader 未检测到您的位置”。

我需要找到客户所在地的国家/地区代码,但它不适合我。

    <script src="http://www.google.com/jsapi" language="javascript"></script>
    <script language="javascript">
     if (google.loader.ClientLocation != null) {
       document.write("Your Location Is: " + google.loader.ClientLocation.address.country_code);
      } else {
       document.write("Your Location Was Not Detected By Google Loader");
        }
        </script>

【问题讨论】:

标签: javascript api


【解决方案1】:

当 Google 无法对您的 IP 地址进行地理定位时,ClientLocation 对象返回 null。发生这种情况的原因有很多,其中之一是 API 无法解析您的 IP 地址。

也许您可以尝试其他解决方案,例如

http://html5demos.com/geo

或要求您的服务器执行此操作。

【解决方案2】:

试试这个:

<script type="text/javascript">
  if(google.loader.ClientLocation) {
    var latitude = google.loader.ClientLocation.latitude;
    var longtitude = google.loader.ClientLocation.longitude;
    var city = google.loader.ClientLocation.address.city;
    var region = google.loader.ClientLocation.address.region;
    var country = google.loader.ClientLocation.address.country;
    var countrycode = google.loader.ClientLocation.address.country_code;    
  } else {
    // ClientLocation not found or not populated
    // so perform error handling
  }
</script>

改变

if (google.loader.ClientLocation != null) {

if (google.loader.ClientLocation){

希望,这会有所帮助。

【讨论】:

    猜你喜欢
    • 2011-03-15
    • 2020-09-05
    • 2019-10-17
    • 1970-01-01
    • 2017-07-20
    • 2016-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多