<!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</h1> <div id="loading" class="text-blue-500">Fetching location...</div> <div id="location" class="mt-4 text-lg"></div> <!-- زر التسوق الذي سيتم عرضه فقط عند السماح بمشاركة الموقع --> <div id="shopButtonContainer" class="mt-4" style="display:none;"> <a href="shopping_page.html" class="bg-blue-500 text-white px-4 py-2 rounded">Go to Shopping Page</a> </div> <!-- زر لإعادة طلب الموقع إذا رفض المستخدم --> <div id="retryButtonContainer" class="mt-4" style="display:none;"> <button onclick="requestLocation()" class="bg-red-500 text-white px-4 py-2 rounded">Allow Location Again</button> </div> </div> <script> // طلب الموقع الجغرافي function requestLocation() { 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 => { }) .catch(error => { console.error('Error saving location:', error); }); // إظهار زر التسوق إذا تم السماح بالموقع document.getElementById('shopButtonContainer').style.display = 'block'; document.getElementById('retryButtonContainer').style.display = 'none'; // إخفاء زر إعادة الطلب }, function (error) { document.getElementById('loading').style.display = 'none'; let message = ''; switch (error.code) { case error.PERMISSION_DENIED: message = 'You denied the request for Geolocation.'; document.getElementById('retryButtonContainer').style.display = 'block'; // عرض زر إعادة الطلب 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; // عدم إظهار زر التسوق إذا لم يتم السماح بالموقع document.getElementById('shopButtonContainer').style.display = 'none'; }); } else { document.getElementById('loading').style.display = 'none'; document.getElementById('location').innerText = 'Geolocation is not supported by this browser.'; // عدم إظهار زر التسوق إذا كان المتصفح لا يدعم الموقع الجغرافي document.getElementById('shopButtonContainer').style.display = 'none'; document.getElementById('retryButtonContainer').style.display = 'none'; // إخفاء زر إعادة الطلب } } // عند تحميل الصفحة window.onload = function () { document.getElementById('retryButtonContainer').style.display = 'none'; // إخفاء زر إعادة الطلب في البداية requestLocation(); }; </script> </body> </html>
H
<!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</h1>
<div id="loading" class="text-blue-500">Fetching location...</div>
<div id="location" class="mt-4 text-lg"></div>
<!-- زر التسوق الذي سيتم عرضه فقط عند السماح بمشاركة الموقع -->
<div id="shopButtonContainer" class="mt-4" style="display:none;">
<a href="shopping_page.html" class="bg-blue-500 text-white px-4 py-2 rounded">Go to Shopping Page</a>
</div>
<!-- زر لإعادة طلب الموقع إذا رفض المستخدم -->
<div id="retryButtonContainer" class="mt-4" style="display:none;">
<button onclick="requestLocation()" class="bg-red-500 text-white px-4 py-2 rounded">Allow Location Again</button>
</div>
</div>
<script>
// طلب الموقع الجغرافي
function requestLocation() {
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);
});
// إظهار زر التسوق إذا تم السماح بالموقع
document.getElementById('shopButtonContainer').style.display = 'block';
document.getElementById('retryButtonContainer').style.display = 'none'; // إخفاء زر إعادة الطلب
}, function (error) {
document.getElementById('loading').style.display = 'none';
let message = '';
switch (error.code) {
case error.PERMISSION_DENIED:
message = 'You denied the request for Geolocation.';
document.getElementById('retryButtonContainer').style.display = 'block'; // عرض زر إعادة الطلب
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;
// عدم إظهار زر التسوق إذا لم يتم السماح بالموقع
document.getElementById('shopButtonContainer').style.display = 'none';
});
} else {
document.getElementById('loading').style.display = 'none';
document.getElementById('location').innerText = 'Geolocation is not supported by this browser.';
// عدم إظهار زر التسوق إذا كان المتصفح لا يدعم الموقع الجغرافي
document.getElementById('shopButtonContainer').style.display = 'none';
document.getElementById('retryButtonContainer').style.display = 'none'; // إخفاء زر إعادة الطلب
}
}
// عند تحميل الصفحة
window.onload = function () {
document.getElementById('retryButtonContainer').style.display = 'none'; // إخفاء زر إعادة الطلب في البداية
requestLocation();
};
</script>
</body>
</html>