添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

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.

Probably you can use the getCurrentPosition function in JavaScript, and put the result to some hidden fields of your Web form:

  • https://learn.microsoft.com/en-us/archive/msdn-magazine/2011/december/building-html5-applications-integrating-geolocation-into-web-applications
  • https://developer.mozilla.org/en-US/docs/Web/API/Geolocation_API/Using_the_Geolocation_API
  • 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>
       <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">
       <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.