【问题标题】:i need to insert latitude and longitude in database from click event but when i click i got zero values in database not an actual values我需要通过点击事件在数据库中插入纬度和经度但是当我点击时我在数据库中得到零值而不是实际值
【发布时间】:2026-02-07 23:10:01
【问题描述】:

使用 ASP.net 和 WebForms 我正在尝试将用户的位置(纬度、经度)存储到数据库中。这提出了问题:

我想通过单击 B 按钮获取谷歌地图的 laqngitude 和 latude 值,我在数据库中得到 0 个值。 我的数据库有两个字段的经度和纬度,它们具有双重数据类型。

这是我第一次尝试的示例代码:

<html xmlns="http://www.w3.org/1999/xhtml"> 
   <head id="Head1" runat="server">
   <title></title>
</head>
<body>
   <script type="text/javascript" src="http://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(18.9300, 72.8200),
               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>

下面是 .aspx 代码隐藏文件。我们在按钮的Click事件期间将经纬度保存到数据库中:

protected void Button1_Click(object sender, EventArgs e)
    {
        //TODO: Ask * how to get these values from browser-land javascript
        Double latitude = Convert.ToDouble(lat.Value);
        Double longitude = Convert.ToDouble(lng.Value);

        SqlConnection con = new SqlConnection();
        con.ConnectionString = "@Data Source=(LocalDB)\v11.0;AttachDbFilename=Database.mdf;Integrated Security=True";

        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();

    }

【问题讨论】:

    标签: javascript c# asp.net google-maps


    【解决方案1】:

    您需要访问在您的服务器端代码中发布的表单。更准确地说是 Request.Form 对象。

    表单上有一个 NameValueCollection 属性,您可以在其中使用字符串键查找查找已发布的表单值

    NameValueCollection nvc = Request.Form;
    string lat,lng; 
    if (!string.IsNullOrEmpty(nvc["lat"])) { 
        lat = nvc["lat"];
     } 
    if (!string.IsNullOrEmpty(nvc["lng"])) { 
        lng = nvc["lng"]; 
    }
    

    【讨论】:

      最近更新 更多