如何在不加载整个Google Maps API的情况下加载Google ClientLocation API?
问题描述:
我想要做的就是找出这个人的IP地址,以便我可以反向地理编码以找出他们从中查看我的网站的纬度和经度。如何在不加载整个Google Maps API的情况下加载Google ClientLocation API?
我可以使用Google ClientLocation API来做到这一点,但目前还不清楚我是否必须加载庞大的Google Map框架才能使用它。
是否可以简单地使用ClientLocation API而无需加载所有Google地图?如果是这样,怎么样?
答
是的,你只需要使用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
- 谷歌.loader.ClientLocation.address.country
- google.loader.ClientLocation.address.country_code
- google.loader.ClientLocation.address.region
答
<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>
你必须这样做,在服务器端的选项,或者你只限于JavaScript客户端基础的解决方案? – Kevin 2010-07-13 11:03:36
最好使用HTML5地理定位。请参阅[是否仍支持google.loader.clientlocation](http://stackoverflow.com/q/14195837/379641)。 – Gan 2013-08-08 15:25:09