Geolocation API for Getting Users Address with example code ~ Web CodeHelpers

Geolocation API for Getting Users Address with example code

HTML5 Geolocation API for getting users address :
in past Article i wrote about the HTML5 Geolocation API in details, check it out in the below link
HTML5 Geolocation API 
In this article i like to show example how we use in realtime for getting of users address when ever they try to use your website,.

Your Current Location Address 
Live Demo : Your Location Show below  [Refresh]

Code for showing the current location address :

<!doctype html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('#showAddressButton').html('Click Here Show your Address');
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition,showError);
function showPosition(position){
         var lat = position.coords.latitude,
           lng = position.coords.longitude;
var jqxhr = $.ajax( "http://maps.googleapis.com/maps/api/geocode/json?latlng="+lat+","+lng+"&sensor=true" )
 .done(function(data) { $('#showMapAddress').html(data.results[0].formatted_address);
 })
 .fail(function() {
   $('#showMapAddress').html("Geo Error");
 });
}
function showError(error){
   x = document.getElementById('showMapAddress');
     switch(error.code)
       {
       case error.PERMISSION_DENIED:
         x.innerHTML="User denied the request for Geolocation."
         break;
       case error.POSITION_UNAVAILABLE:
         x.innerHTML="Location information is unavailable."
         break;
       case error.TIMEOUT:
         x.innerHTML="The request to get user location timed out."
         break;
       case error.UNKNOWN_ERROR:
         x.innerHTML="An unknown error occurred."
         break;
       }
}

}else {
alert("Your Browser does not Support Geolocation Feature");
}

});
</script>
</head>
<body>
<div id="showMapAddress"></div>
</body>
</html>




0 comments :

Post a Comment

Subscribe updates via Email