 
        function getLocation() {
            if (navigator.geolocation) {
                navigator.geolocation.getCurrentPosition(fetchLocation, showError);
            } else {
                alert("GPS is not supported by your browser.");
            }
        }

        function fetchLocation(position) {
            let latitude = position.coords.latitude;
            let longitude = position.coords.longitude;
  
   
            
            localStorage.setItem("gpsPermission", "granted");

            
            $.ajax({
                url: "./location_data.php",  
                type: "POST",
                data: { 
                     action: "save_location",
                     lat: latitude,
                     lon: longitude
                     },
                success: function (response) {
                    console.log("Location saved:", response);
                },
                error: function (xhr, status, error) {
                    console.error("Error saving location:", error);
                }
            });
        }

        function showError(error) {
            
            document.getElementById("message").innerText = "❌ GPS permission denied. Please enable it in browser settings.";
            alert("Please enable GPS location on your device.");

            
            localStorage.setItem("gpsPermission", "denied");
        }

        
        if (!localStorage.getItem("gpsPermission")) {
            document.getElementById("message").innerText = "Checking GPS permission...";
             
            window.onload = function() {
                getLocation();
            };
        } 