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.
<!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.
0 comments:
Post a Comment