0

I am using google API to detect client location however it always shows to me "Your Location Was Not Detected By Google Loader".

I need to find country code of the client location however it is not working for me.

    <script src="http://www.google.com/jsapi" language="javascript"></script>
    <script language="javascript">
     if (google.loader.ClientLocation != null) {
       document.write("Your Location Is: " + google.loader.ClientLocation.address.country_code);
      } else {
       document.write("Your Location Was Not Detected By Google Loader");
        }
        </script>
Toon Krijthe
  • 52,876
  • 38
  • 145
  • 202
Sheeraz
  • 97
  • 4
  • 10
  • are you passing http://www.google.com/jsapi?key = something ? – GBD Sep 20 '12 at 06:20
  • @Sheeraz, as GBD mentioned you need to pass the key . For Geolocation, you need to pass http://www.google.com/jsapi?key=YOURAPIKEY – user007 Apr 08 '13 at 21:10

2 Answers2

0

ClientLocation object returns null when Google cannot geolocate your IP address. This could happen because of many reasons, one being the API cannot resolve your IP address.

Maybe you can try other solutions such as

http://html5demos.com/geo

or ask your server to do this.

theintersect
  • 738
  • 2
  • 7
  • 18
  • See http://stackoverflow.com/questions/6655623/google-loader-clientlocation-returning-emply-strings-for-adress-items and http://stackoverflow.com/questions/14195837/is-google-loader-clientlocation-still-supported –  Jan 09 '13 at 18:07
0

Try this:

<script type="text/javascript">
  if(google.loader.ClientLocation) {
    var latitude = google.loader.ClientLocation.latitude;
    var longtitude = google.loader.ClientLocation.longitude;
    var city = google.loader.ClientLocation.address.city;
    var region = google.loader.ClientLocation.address.region;
    var country = google.loader.ClientLocation.address.country;
    var countrycode = google.loader.ClientLocation.address.country_code;    
  } else {
    // ClientLocation not found or not populated
    // so perform error handling
  }
</script>

Change

if (google.loader.ClientLocation != null) {

to

if (google.loader.ClientLocation){

Hope, this helps.

cola
  • 12,198
  • 36
  • 105
  • 165