如何在不加载整个Google Maps API的情况下加载Google ClientLocation API?

Sar*_*Bee 4 google-maps geocoding geolocation

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

我可以使用Google ClientLocation API做到这一点但我不清楚我是否需​​要加载庞大的Google Map框架才能使用它.

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

Fra*_*ser 12

是的,您只需要在google.loader命名空间中使用ClientLocation对象,因此您无需引用maps 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>
Run Code Online (Sandbox Code Playgroud)

可用的属性是

  • 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