Retrieving lat/long IP address

Retrieve the user’s current geolocation(lat/long) using current IP address of the user.

Retrieving User’s lat/long from their IP address using javascript:-

1.Using google clientlocation loader AP

    
        if(google.loader.ClientLocation)
        {
            lat = google.loader.ClientLocation.latitude;
            lon = google.loader.ClientLocation.longitude;
        }
        else
        {
            //do error handling
        }
    

Note:- Here key is not mandatory.
It is supported by almost all browser but it is less accurate.

2)Using HTML5 Geolocation API

        if(navigator.geolocation)
        {
            navigator.geolocation.getCurrentPosition(function(position)
            {
                lat = position.coords.latitude;
                lon = position.coords.longitude;
            },
            function errorCallback(error)
            {
                //do error handling
                alert(error.code);
                alert(error.message);
            },
            {
                maximumAge:Infinity,
                timeout:555000
            });
        }
        else
        {
            //do error handling
        }
    

It is not supported by the browsers which do not support HTML5 but it gives the accurate value.

3)Using Google Gears API
Download gears_init.js from http://code.google.com/apis/gears/gears_init.js

    
        if(google.gears)
        {
            var geo = google.gears.factory.create('beta.geolocation');
            geo.getCurrentPosition(function(position)
            {
                lat = position.latitude;
                lon = position.longitude;
            });
           
        }
        else
        {
            //do error handling
        }
    

To use google gears you need to have google gear installed,
which you can find at http://gears.google.com/ .

Note:- Different browser supports different API so to get the result in all browsers
use the code like given below:-

      
     
        var error_val = 0;
        //Try W3C Geolocation (Preferred)
        if(navigator.geolocation)
        {
            navigator.geolocation.getCurrentPosition(function(position)
            {
                document.getElementById('lat').value = position.coords.latitude;
                document.getElementById('longitude').value = position.coords.longitude;
                error_val = 2;
            },
            function errorCallback(error)
            {
                //do error handling
                alert(error.code);
                alert(error.message);
                error_val = 1;
            },
            {
                maximumAge:Infinity,
                timeout:555000
            });
        }
       
        // Try Google Gears Geolocation
        //else
        if(error_val < 2)
        {
            if(google.gears)
            {
                var geo = google.gears.factory.create('beta.geolocation');
                geo.getCurrentPosition(function(position)
                {
                    document.getElementById('lat').value = position.latitude;
                    document.getElementById('longitude').value = position.longitude;
                });
               
            }
       
            //Try Google jsapi
            else if(google.loader.ClientLocation)
            {
                document.getElementById('lat').value = google.loader.ClientLocation.latitude;
                document.getElementById('longitude').value = google.loader.ClientLocation.longitude;
            }
            else
            {
                //DO ERROR HANDLING
            }
        }
     
150 150 Burnignorance | Where Minds Meet And Sparks Fly!