Jaya Prakash

Jaya Prakash

  • 511
  • 2.3k
  • 65k

Getting Latitude and logitude of User based on location

Dec 26 2023 4:01 AM

 This is my ajax script for getting user latitude and logitude based on location it is working properly in my localhost but in my beta application unable to get the values pls help me to fix it any other suggestion pls thanks in advance.
 




    <script type="text/javascript">


        
        function getLocation() {
            debugger
            if (navigator.geolocation) {
                navigator.geolocation.getCurrentPosition(showPosition);
            } else {
                alert("Geolocation is not supported by this browser.");
            }
        }

        function showPosition(position) {
            debugger
            var latitude = position.coords.latitude;
            var longitude = position.coords.longitude;

            // Send the location data to the server using AJAX
            $.ajax({
                type: "POST",
                url: "Login.aspx/SaveLocation",
                data: JSON.stringify({ latitude: latitude, longitude: longitude }),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (response) {
                    console.log("Location saved successfully:", response);
                },
                error: function (xhr, textStatus, errorThrown) {
                    console.error("Error saving location:", textStatus, errorThrown);
                    console.log("Response Text:", xhr.responseText);
                }
            });


        }

    </script>


<script>
 getLocation();
</script>




 public static  string Latitude;
        public static  string Longitude;


 [WebMethod]
        public static void SaveLocation(string latitude, string longitude)
        {
            Latitude = latitude;
            Longitude = longitude;
        }

 


Answers (1)