经度和纬度未分配给HiddenFields [更新]

问题描述 投票:0回答:2

[更新]我试图通过在推送到数据库的aspx文件的后端(.cs)上进行地理编码来获得纬度和经度。但是,当我在Geocode函数中尝试console.log时,HiddenFields没有被赋值,这给了我未定义的。

我尝试了很多解决方案,但没有任何解决方案,或者问题在没有任何解决方案的情我尝试过使用: 1. Request.Form [Latitude.UniqueID] 2.使用SetTimeOut(在地理编码函数上)3。在后端使用hiddenField的.Value / .Value.toString()

不幸的是,这一切都没有奏效。

地理编码功能

        var hiddenlat = $("#<%= hiddenlat.ClientID %>");
        var hiddenlng = $("#<%= hiddenlng.ClientID %>");

         function getLatLng() {
         ...

        geocoder.geocode({ 'address': "'" + address + "'" }, function (results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                console.log("postal code in post.aspx is ", <%= postalcode 
                 %>);
                var latitude = results[0].geometry.location.lat();
                var longitude = results[0].geometry.location.lng();
                hiddenlat.val(latitude);
                hiddenlng.val(longitude);
                console.log(hiddenlat.val()); // undefined
                console.log(hiddenlng.val()); // undefined
                var data = { "lat": latitude, "lng": longitude };
              } else {
                console.log('request fail');
            }

        });

        return true;

    }

这是隐藏的领域

      <asp:HiddenField ID="hiddenlat" runat="server" />
        <asp:HiddenField ID="hiddenlng" runat="server" />

用于发布的按钮

         <asp:Button ID="btnSubmit" runat="server" CssClass ="btn btn- 
          success" Text="List" OnClientClick="getLatLng()" 
           OnClick="btnSubmit_Click"/>

后端代码

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        int lat = Convert.ToInt32(hiddenlat.Value);
        int lng = Convert.ToInt32(hiddenlng.Value);
        ........

          //Pushing of data into database
          uploadDao.listitem(user, date, item, rtype, ptype, paper, metal, 
           batt, elec, weight, desc, add, images[0], images[1], images[2], 
          images[3], unitno, postalcode, qty, district, Convert.ToInt32(lat), 
           Convert.ToInt32(lng));

            Response.Redirect("SellerListing.aspx?user=" + user);
    }

[更新]当我在地理编码功能中设置Hiddenfield的值时,HiddenFields内的值是Undefined。我可以知道为什么它未定义?我尝试了Console.log纬度和经度,两者都返回一个值。

javascript asp.net geocoding
2个回答
0
投票

首先,你需要测试geocode功能。要测试geocode正在返回latitudelongitude,您需要将其登录到控制台,如下所示。

function getLatLng(){
    //... your code.
    var latitude = results[0].geometry.location.lat();
    var longitude = results[0].geometry.location.lng();

    console.log("Latitude:" + latitude + ' and Longtitude:' + longtitude );


    //Return false in getLatLng()
    return false;

}

0
投票

HTML

<input type="text"  id="txtAddress" />
<input type="button"  value="Get Lat/Long"  id="btnGetLatLong"/>

<asp:Button Text="Save" runat="server" ID="btnSaveOnSever"  OnClick="btnSaveOnSever_Click" />

<asp:HiddenField   runat="server" ID="hdnLat" ClientIDMod="static"/>
<asp:HiddenField   runat="server" ID="hdnLong" ClientIDMod="static" />

使用Javascript

    $('#btnGetLatLong').click(function() {

        var geocoder = new google.maps.Geocoder();
        var address = $('#txtAddress').val();

        geocoder.geocode( { 'address': address}, function(results, status) {

            if (status == google.maps.GeocoderStatus.OK) {
            var latitude = results[0].geometry.location.lat();
            var longitude = results[0].geometry.location.lng();

                $('#hdnLat').val(latitude);
                $('#hdnLong').val(longitude);
                alert("Success");
            } else {
                alert("faild");
            } 
        }); 

    });

服务器端

protected void btnSaveOnSever_Click(object sender, EventArgs e)
{
    decimal lat = Convert.ToDecimal(hdnLat.Value);
    decimal lng = Convert.ToDecimal(hdnLong.Value);
    ........

      //Pushing of data into database
      uploadDao.listitem(user, date, item, rtype, ptype, paper, metal,
       batt, elec, weight, desc, add, images[0], images[1], images[2],
      images[3], unitno, postalcode, qty, district, lat,
       lng);

    Response.Redirect("SellerListing.aspx?user=" + user);
}
© www.soinside.com 2019 - 2024. All rights reserved.