How to load Google ClientLocation API without loading the whole Google Maps API?

情到浓时终转凉″ 提交于 2019-12-05 01:15:50

问题


All I want to do is find out the person IP address so that I can reverse geocode it to find out their latitude and longitude from which they are viewing my web site from.

I can do that using Google ClientLocation API but it's unclear to me if I have to load the huge Google Map framework just to use it.

Is it possible to simply use the ClientLocation API without having to load all of Google Maps? If so, how?


回答1:


Yes, you only need to use the ClientLocation object in the google.loader namespace so you need not reference the maps api or anything else. For example.

<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>

The properties available are

  • 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



回答2:


<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>


来源:https://stackoverflow.com/questions/3233767/how-to-load-google-clientlocation-api-without-loading-the-whole-google-maps-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!