<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Get User Location</title>
<link href
="https://c...content-available-to-author-only...r.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel
="stylesheet"> </head>
<body class="bg-gray-100">
<div class="container mx-auto p-4">
<h1 class="text-3xl font-semibold mb-4">Get User Location Automatically</h1>
<div id="loading" class="text-blue-500">Fetching location...</div>
<div id="location" class="mt-4 text-lg"></div>
</div>
<script>
// عند تحميل الصفحة، طلب إذن الموقع تلقائيًا
window.onload = function () {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function (position) {
const latitude = position.coords.latitude;
const longitude = position.coords.longitude;
// عرض الإحداثيات
document.getElementById('loading').style.display = 'none';
document.getElementById('location').innerText = `Latitude: ${latitude}, Longitude: ${longitude}`;
// إرسال الموقع إلى الخادم باستخدام Fetch API
fetch('save_location.php', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ latitude, longitude })
})
.then(response => response.json())
.then(data => {
console
.log('Location saved:', data
); })
.catch(error => {
console.error('Error saving location:', error);
});
}, function (error) {
document.getElementById('loading').style.display = 'none';
let message = '';
switch (error.code) {
case error.PERMISSION_DENIED:
message = 'User denied the request for Geolocation.';
break;
case error.POSITION_UNAVAILABLE:
message = 'Location information is unavailable.';
break;
case error.TIMEOUT:
message = 'The request to get user location timed out.';
break;
default:
message = 'An unknown error occurred.';
}
document.getElementById('location').innerText = message;
});
} else {
document.getElementById('loading').style.display = 'none';
document.getElementById('location').innerText = 'Geolocation is not supported by this browser.';
}
};
</script>
</body>
</html>