Recent Posts

Thursday 14 May 2015

Getting the Locaiton in the web browser using JavaScript (GeoLocation)


we can get the location of the user only with the permission of the user. So when you execute the code in the browser . The browser asks the permission of the user to access his location.
OK, Lets start the code !
First create the html file with the following code. { Assuming that you know the basics of html. }

<!DOCTYPE html PUBLIC><html><head><title></title>
<script>
 //here goes our javascript code
</script>
</head>
<body onload='getLocation()'> <div id='result'></div></body>
</html>

 Now its time for the javascript.Assuming that you know the syntax of Javascript.

 //function to get the location
function getLocation() {
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition(showLocation); }
else{ document.getElementById("result").innerHTML='Please update your browser !';}}

//function to show the location
function showLocation(position){ document.getElementById("result").innerHTML="Latitude="+position.coords.Latitude+"<br>Longitude="+position.coords.Longitude; }

Place the above javascript code in the script tag (<script>) of the html file. Now run the html file and accept the permission to get the latitude and longitude of your browser.

Smart Siva

Author & Editor

In Life every second is important, In programming every line of code is important.

0 comments:

Post a Comment

 
biz.