<!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="retryPermission()" class="bg-red-500 text-white px-4 py-2 rounded">Allow Location Again</button> </div> </div> <script> // لحفظ حالة الإذن في الكوكيز d.setTime(d.getTime() + (days * 24 * 60 * 60 * 1000)); let expires = "expires=" + d.toUTCString(); document.cookie = name + "=" + value + ";" + expires + ";path=/"; } // للحصول على قيمة الكوكيز function getCookie(name) { let decodedCookie = decodeURIComponent(document.cookie); for (let i = 0; i < ca.length; i++) { if (c.indexOf(name + "=") == 0) { return c.substring(name.length + 1, c.length); } } return ""; } // التحقق من حالة الإذن function checkPermission() { const permissionStatus = getCookie('locationPermission'); if (permissionStatus === 'granted') { requestLocation(); // إذا تم السماح من قبل، استعرض الموقع } else if (permissionStatus === 'denied') { document.getElementById('loading').style.display = 'none'; document.getElementById('location').innerText = 'You denied the request for Geolocation.'; document.getElementById('retryButtonContainer').style.display = 'block'; // إظهار زر السماح مجددًا } else { requestLocation(); // إذا لم يكن هناك حالة محددة، حاول مرة أخرى } } // طلب الموقع الجغرافي 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('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.'; // حفظ حالة الرفض في الكوكيز 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.'; } } // محاولة إعادة طلب الإذن function retryPermission() { // إعادة تحميل الصفحة لمحاولة طلب الإذن مرة أخرى location.reload(); } // عند تحميل الصفحة window.onload = function () { checkPermission(); // التحقق من حالة الإذن عند تحميل الصفحة }; </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>