2010-07-13 71 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> 
相关问题