How can I get Latitude and Longitude in c# asp.net webforms from codebehind

Soykan Ozcelik 0 Reputation points
2023-04-26T06:35:44.8033333+00:00

How can I get Latitude and Longitude in c# asp.net web forms from codebehind? or another suggested way js or?

My goal find lat and lon insert that values to the table while sales operation inserting or updating records.

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,371 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,497 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Lan Huang-MSFT 27,796 Reputation points Microsoft Vendor
    2023-04-26T08:43:00+00:00

    Hi @Soykan Ozcelik,

    It is not very clear how you get the latitude and longitude, but you can refer to the following method.

    Add asp hidden fields, with runat="server" to hold the lat and lng. When you get the lat and lng in your javascript simply populate the hidden fields with those values. Then you can reference the hidden fields values directly in your OnClick handler.

    <html xmlns="http://www.w3.org/1999/xhtml"> 
       <head id="Head1" runat="server">
       <title></title>
    </head>
    <body>
       <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
       <script type="text/javascript">
           var long = 0;
           var lat = 0;
           window.onload = function () {
               var mapOptions = {
                   center: new google.maps.LatLng(20.9600, 80.0000),
                   zoom: 14,
                   mapTypeId: google.maps.MapTypeId.ROADMAP
               };
               var infoWindow = new google.maps.InfoWindow();
               var latlngbounds = new google.maps.LatLngBounds();
               var map = new google.maps.Map(document.getElementById("dvMap"), mapOptions);
               google.maps.event.addListener(map, 'click', function (e) {
                   long = e.latLng.lng();
                   lat = e.latLng.lat();
                   document.getElementById("lat").value = lat;
                   document.getElementById("lng").value = long;
                   alert("Latitude: " + lat + "\r\nLongitude: " + long);
               });
           }
       </script>
    
    <form id="myForm" runat="server">
       <div id="dvMap" style="width: 300px; height: 300px">
       </div>
       <asp:HiddenField ID="lat" runat="server" />
       <asp:HiddenField ID="lng" runat="server" />
       <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
    
    </form>
    </body>
        </html>
    
     protected void Button1_Click(object sender, EventArgs e)
            {
               
                Double latitude = Convert.ToDouble(lat.Value);
                Double longitude = Convert.ToDouble(lng.Value);
    
                SqlConnection con = new SqlConnection();
                con.ConnectionString = "****";
    
                string query1 = "insert into Courses(longi,lati) values (@lati, @longi)";
    
                SqlCommand cmd1 = new SqlCommand(query1, con);
                cmd1.Parameters.AddWithValue("@lati", latitude);
                cmd1.Parameters.AddWithValue("@longi", longitude);
    
                con.Open();
                cmd1.ExecuteNonQuery();
                con.Close();
    
            }
    

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments