Tuesday, 19 March 2013

how to find current location using javascript & jquery

<html>
<head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.js">
    </script>
    <script src="http://j.maxmind.com/app/geoip.js"></script>
   <script src="http://maps.google.com/maps/api/js?sensor=false"></script>

<script>

    // wire up button click
    function fun1()
    {
        
        // test for presence of geolocation
        if (navigator && navigator.geolocation)
         {
            // make the request for the user's position
            navigator.geolocation.getCurrentPosition(geo_success, geo_error);
        }
         else
         {
            // use MaxMind IP to location API fallback
            printAddress(geoip_latitude(), geoip_longitude(), true);
        }
    }

function geo_success(position)
 {
    printAddress(position.coords.latitude, position.coords.longitude);
}

function geo_error(err)
{
    // instead of displaying an error, fall back to MaxMind IP to location library
    printAddress(geoip_latitude(), geoip_longitude(), true);
}

// use Google Maps API to reverse geocode our location
function printAddress(latitude, longitude, isMaxMind)
{
        // set up the Geocoder object
    var geocoder = new google.maps.Geocoder();

    // turn coordinates into an object
    var yourLocation = new google.maps.LatLng(latitude, longitude);

     // find out info about our location
    geocoder.geocode({ 'latLng': yourLocation }, function (results, status)
    {
        if (status == google.maps.GeocoderStatus.OK)
        {
            if (results[0])
            {
                $('body').append('<p>Your Address:<br />' +
                    results[0].formatted_address + '</p>');
            } else
            {
                error('Google did not return any results.');
            }
        }
        else
        {
            error("Reverse Geocoding failed due to: " + status);
        }
    });

  
}

function error(msg) {
    alert(msg);
}
</script>

</head>
<body>
<b id="my"></b>
<input type="button" id="go" value="Click Me To Find Your Address" onclick="fun1()">
</body>
<script>
</script>
</html>

No comments:

Post a Comment