我想要做的就是找出這個人的IP地址,以便我可以反向地理編碼以找出他們從中查看我的網站的緯度和經度。如何在不加載整個Google Maps API的情況下加載Google ClientLocation API?
我可以使用Google ClientLocation API來做到這一點,但目前還不清楚我是否必須加載龐大的Google Map框架才能使用它。
是否可以簡單地使用ClientLocation API而無需加載所有Google地圖?如果是這樣,怎麼樣?
我想要做的就是找出這個人的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>
可用的屬性是
<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