【问题标题】:How to load Google ClientLocation API without loading the whole Google Maps API?如何在不加载整个 Google Maps API 的情况下加载 Google ClientLocation API?
【发布时间】:2011-03-15 02:37:00
【问题描述】:

我想要做的就是找出这个人的 IP 地址,以便我可以对其进行反向地理编码,以找出他们查看我网站的纬度和经度。

我可以使用 Google ClientLocation API 做到这一点,但我不清楚是否必须加载庞大的 Google 地图框架才能使用它。

是否可以简单地使用 ClientLocation API 而无需加载所有 Google 地图?如果有,怎么做?

【问题讨论】:

标签: google-maps geolocation geocoding


【解决方案1】:

是的,您只需要使用 google.loader 命名空间中的 ClientLocation 对象,因此您不需要引用地图 api 或其他任何东西。例如。

<script src="http://www.google.com/jsapi" language="javascript"></script>
<script language="javascript">
if (google.loader.ClientLocation != null) {
  alert(google.loader.ClientLocation.address.city);
} else {
  alert("Not found");
}
</script>

可用的属性是

  • google.loader.ClientLocation.latitude
  • google.loader.ClientLocation.longitude
  • google.loader.ClientLocation.address.city
  • google.loader.ClientLocation.address.country
  • google.loader.ClientLocation.address.country_code
  • google.loader.ClientLocation.address.region

【讨论】:

  • 嗨,每次我登陆 else 块时,请您指导我。
  • @Abhishek 当 Google 无法定位 IP 地址时,clientLocation 对象为空,这可能是由于多种原因,例如 API 无法解析您的 IP 地址。
【解决方案2】:
<html>
    <head>
        <script language="JavaScript" src="http://j.maxmind.com/app/geoip.js"></script>
        <br>Country Name:
        <script language="JavaScript">document.write(geoip_country_name());</script>
        <br>City:
        <script language="JavaScript">document.write(geoip_city());</script>
        <br>Latitude:
        <script language="JavaScript">document.write(geoip_latitude());</script>
        <br>Longitude:
        <script language="JavaScript">document.write(geoip_longitude());</script>
    </head>
    <body>
        Hope this will be useful for you
    </body>
</html>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-25
    • 1970-01-01
    • 1970-01-01
    • 2023-02-01
    • 1970-01-01
    • 2011-07-14
    • 2013-03-25
    • 1970-01-01
    相关资源
    最近更新 更多