locked
Save GPS coordinates when visiting the page (ASP.NET/JS) RRS feed

  • Question

  • User823886193 posted

    I'm developing an app through ASP.NET and C # and through JS I get the Geolocation, the problem is that I should save this data in an SQL Server database, but I don't know how to do it. Because even trying to insert the SQL statement in the "LoadComplete" event is not working. I tried also to insert the data of the coordinates in a HiddenField and I tried to insert the instruction SQL in the event "ValueChange" but it doesn't work. Do you have any advice?

    Obviously all this must be done when the page is visited.

    Then I will try to be as clear as possible.

    I retrieve the position of the user visiting the page and insert the recovered values into a HiddenField:

    <div>
    
            <asp:HiddenField ID="HiddenField1" runat="server" Value="" />
    
            <script>
                var x = document.getElementById("HiddenField1");
                function getLocation() {
                    if (navigator.geolocation) {
                        navigator.geolocation.getCurrentPosition(showPosition);
                    }
                    else { x.value = "Geolocation is not supported by this browser."; }
                }
                function showPosition(position) {
                    x.value = "Latitude: " + position.coords.latitude +
                        "<br>Longitude: " + position.coords.longitude; + "<br>Accuracy" + position.coords.accuracy; 
    
                }
    
            </script>
        </div>

    Now I should save the data in the Hidden Field in an SQL database, without the end user clicking on a button or anything else.

    the problem is that I can't find an event to exploit, because for example using the LoadComplete event:

            protected void Page_Load(object sender, EventArgs e)
        {
            var cnnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            var cmd = "insert into Insert values(@Location)";
            using (SqlConnection cnn = new SqlConnection(cnnString))
            {
                using (SqlCommand cmd = new SqlCommand(cmd, cnn))
                {
                    cmd.Parameters.AddWithValue("@Location", HiddenField1.Value.ToString);
    
                    cnn.Open();
                    cmd.ExecuteNonQuery();
                }
            }
        }

    However this event cannot work as it is generated before the function present in the JS script is completed.

    So what other event could I use to save the position?

    Thank You

    Wednesday, April 17, 2019 3:18 AM

Answers

  • User-893317190 posted

    Hi PasqualeZ ,

    I suggest you could use ajax to send a http request  with the position data to server.

    Below is my code.

    <form id="form1" runat="server">
            <div>
            </div>
             <script src="https://code.jquery.com/jquery-3.3.1.js"></script>
            <script>
              
                function getLocation() {
                    if (navigator.geolocation) {
                        navigator.geolocation.getCurrentPosition(showPosition);
                     
                    }
                    else {alert( "Geolocation is not supported by this browser."); }
                }
                function showPosition(position) {
    
    
                      $.ajax({
                        url: '/Services/MyService.asmx/getLocation',
                        type: 'post',
                          dataType: 'json',
                          data: JSON.stringify( {
                             
                              location: "Latitude: " + position.coords.latitude +
                        ",Longitude: " + position.coords.longitude + ",Accuracy" + position.coords.accuracy}),
                        contentType: 'application/json',
                        success: function (data) {
                            alert('Success: ' + data);
                        },
                        error: function (error) {
                            alert(error.statusText);
                        }
                    });
                    
    
                }
    
                getLocation();
       
    
            </script>

    My webmethod.

    [WebMethod]
          
            public string getLocation(string location)
            {
    
                
                return location ;
    
            }
    

    The result.

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 17, 2019 7:31 AM

All replies

  • User-893317190 posted

    Hi PasqualeZ ,

    I suggest you could use ajax to send a http request  with the position data to server.

    Below is my code.

    <form id="form1" runat="server">
            <div>
            </div>
             <script src="https://code.jquery.com/jquery-3.3.1.js"></script>
            <script>
              
                function getLocation() {
                    if (navigator.geolocation) {
                        navigator.geolocation.getCurrentPosition(showPosition);
                     
                    }
                    else {alert( "Geolocation is not supported by this browser."); }
                }
                function showPosition(position) {
    
    
                      $.ajax({
                        url: '/Services/MyService.asmx/getLocation',
                        type: 'post',
                          dataType: 'json',
                          data: JSON.stringify( {
                             
                              location: "Latitude: " + position.coords.latitude +
                        ",Longitude: " + position.coords.longitude + ",Accuracy" + position.coords.accuracy}),
                        contentType: 'application/json',
                        success: function (data) {
                            alert('Success: ' + data);
                        },
                        error: function (error) {
                            alert(error.statusText);
                        }
                    });
                    
    
                }
    
                getLocation();
       
    
            </script>

    My webmethod.

    [WebMethod]
          
            public string getLocation(string location)
            {
    
                
                return location ;
    
            }
    

    The result.

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 17, 2019 7:31 AM
  • User823886193 posted

    Thank you so much! Work! 

    Thursday, April 18, 2019 12:32 AM