You can easily locate user position using Geolocation API. It is used to get the geographical position of user. However it is much more accurate for devices like iphone with GPS. We can find the user position by using etCurrentPosition() method. So first check if Geolocation is supported if yes run the getCurrentPostion() method if successful it returns a coordinate function of an object which is specified in parameter showposition which calls the function and output the latitude and longitude.
<script>
var x = document.getElementById("check");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported.";
}
}
function showPosition(position) {
x.innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}
</script>
0 Comment(s)