Ingin tahu cara menampilkan latitude dan longitude pengunjung website?Kita bisa gunakan Geolocation yang dijelaskan pada kode berikut ini. Beri nama saja dengan geo.php dan kita simpan di localhost kita.
Berikut ini contoh penerapannya
Berikut ini contoh penerapannya
Location data here
<!DOCTYPE html>
<html>
<head>
<script>
/*Location functions here*/
function getUserLocation() {
//check if the geolocation object is supported, if so get position
if (navigator.geolocation)
navigator.geolocation.getCurrentPosition(displayLocation, displayError);
else
document.getElementById("locationData").innerHTML = "Sorry - your browser doesn't support geolocation!";
}
function displayLocation(position) {
//build text string including co-ordinate data passed in parameter
var displayText = "User latitude is " + position.coords.latitude + " and longitude is " + position.coords.longitude;
//display the string for demonstration
document.getElementById("locationData").innerHTML = displayText;
}
function displayError(error) {
//get a reference to the HTML element for writing result
var locationElement = document.getElementById("locationData");
//find out which error we have, output message accordingly
switch(error.code) {
case error.PERMISSION_DENIED:
locationElement.innerHTML = "Permission was denied";
break;
case error.POSITION_UNAVAILABLE:
locationElement.innerHTML = "Location data not available";
break;
case error.TIMEOUT:
locationElement.innerHTML = "Location request timeout";
break;
case error.UNKNOWN_ERROR:
locationElement.innerHTML = "An unspecified error occurred";
break;
default:
locationElement.innerHTML = "Who knows what happened...";
break;
}}
</script>
</head>
<body>
<input type="button" value="get location" onclick="getUserLocation()"/>
<div id="locationData">
Location data here
</div>
</body>
</html>

أزال المؤلف هذا التعليق.
ردحذفإرسال تعليق