Welcome to FindNerd, today we are going to discuss how to get latitude and longitude of the user's position.
If you want to get latitude and longitude of the user's position, then you should use getLocation() method.
In a web application, the geolocation API permits the user to provide their location. To report location information you can ask user for privacy reasons. The Geolocation() method is much more accurate for devices with GPS, like iPhone.
You can see below example:
!DOCTYPE html>
<html>
<body>
<p>Click on the submit button to get your location coordinates.</p>
<!-- define here a button and call getLocation() -->
<button onclick="getLocation()">Submit</button>
<!-- define here id of a paragraph in which you can see your coordinates-->
<p id="LatLong"></p>
<script>
//define a variabe x and pass paragraph id
var x = document.getElementById("LatLong");
//here call getLocation() method
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
//here get your Latitude and Longitude
function showPosition(position) {
x.innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}
</script>
</body>
</html>
0 Comment(s)