4

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?

SarahBee
  • 41
  • 1
  • 2
  • Do you have the option to do this on server side, or are you limited only to a javascript client based solution ? – Kevin Jul 13 '10 at 11:03
  • It is better to use HTML5 Geolocation. See [Is google.loader.clientlocation still supported](http://stackoverflow.com/q/14195837/379641). – Gan Aug 08 '13 at 15:25

2 Answers2

12

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
Fraser
  • 15,275
  • 8
  • 53
  • 104
  • 1
    Hi, Every time I am landing in else block can you please guide me. – Pedantic Jan 10 '12 at 10:39
  • @Abhishek The clientLocation object is null whenever Google is unable to geolocate the IP address, this could be for multiple reasons such as the API not being able to resolve your IP address. – Fraser Jan 19 '12 at 03:39
-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>
Prashant
  • 692
  • 2
  • 11
  • 26