Geolocation API in HTML5
HTML5 includes a new API for obtaining the user's location, called the Geolocation API. Here's an example of how to use the Geolocation API to display the user's latitude and longitude:
php
<!DOCTYPE html>
<html>
<head>
<title>My Location</title>
<script>
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
alert("Geolocation is not supported by this browser.");
}
}
function showPosition(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
document.getElementById("location").innerHTML = "Latitude: " + latitude + "<br>Longitude: " + longitude;
}
</script>
</head>
<body onload="getLocation()">
<p id="location">Getting location...</p>
</body>
</html>
In this example, we use the Geolocation API to obtain the user's location, and the getCurrentPosition() method to get the current latitude and longitude. We then use JavaScript to display the latitude and longitude on the web page.
No comments:
Post a Comment