2010-07-13 74 views
4

我想要做的就是找出這個人的IP地址,以便我可以反向地理編碼以找出他們從中查看我的網站的緯度和經度。如何在不加載整個Google Maps API的情況下加載Google ClientLocation API?

我可以使用Google ClientLocation API來做到這一點,但目前還不清楚我是否必須加載龐大的Google Map框架才能使用它。

是否可以簡單地使用ClientLocation API而無需加載所有Google地圖?如果是這樣,怎麼樣?

+0

你必須這樣做,在服務器端的選項,或者你只限於JavaScript客戶端基礎的解決方案? – Kevin 2010-07-13 11:03:36

+0

最好使用HTML5地理定位。請參閱[是否仍支持google.loader.clientlocation](http://stackoverflow.com/q/14195837/379641)。 – Gan 2013-08-08 15:25:09

回答

12

是的,你只需要使用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
+1

嗨,每次我在其他地方着陸時,請引導我。 – Pedantic 2012-01-10 10:39:39

+0

@Ahishek每當Google無法確定IP地址的地理位置時,clientLocation對象爲空,這可能是由於多種原因,例如API無法解析您的IP地址。 – Fraser 2012-01-19 03:39:21

-1
<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> 
相關問題