【问题标题】:how to pass a javascript function value to a label in webform如何将javascript函数值传递给webform中的标签
【发布时间】:2014-10-09 09:29:04
【问题描述】:

好吧,我正在尝试查找给定地址的纬度和经度。地址来自文本区域,然后当我单击按钮时,它会给我输入地址的纬度和经度作为警报。我是使用以下脚本:

<script type="text/javascript" src="http://maps.google.com/maps/api/js?v=3&sensor=false&key=AIzaSyDVk87DSFyZlxmYAM8NPNr8sZPN60FYLNA"></script>
<script type="text/javascript">
function calculateCoordinates() {
    var lblStreetAddress = document.getElementById('<%= lblStreetAddress.ClientID%>');
    var lblLandmark = document.getElementById('<%= lblLandmark.ClientID%>');
    var lblCity = document.getElementById('<%= lblCity.ClientID%>');
    var lblState = document.getElementById('<%= lblState.ClientID%>');
    var lblZipCode = document.getElementById('<%= lblZipCode.ClientID%>');
    var txtLatitude = document.getElementById('<%= txtLatitude.ClientID%>');
    var txtLongitude = document.getElementById('<%= txtLongitude.ClientID%>');

                                var address = lblStreetAddress.value + ', ';
                                address += lblLandmark.value + ', ';
                                address += lblCity.value + ', ';
                                address += lblZipCode.value + ', ';
                                address += lblState.value;

    var geocoder;
    geocoder = new google.maps.Geocoder();

    geocoder.geocode({ address: address }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            var location = results[0].geometry.location;
            txtLatitude.value = location.lat();
            txtLongitude.value = location.lng();
        }
        else
            alert(searchString + ' - not found');
    });
}
 </script>
 <asp:TextBox id="txtLatitude" runat="server" Width="100px"></asp:TextBox>                      
 <asp:TextBox id="txtLongitude" runat="server" Width="100px"></asp:TextBox>

这绝对没问题,但我真正想要的是 1.我想使用标签而不是文本区域,因为地址应该来自数据库。 2.我想将纬度和经度值传递给几个标签,以便我可以将它们存储回数据库中。

有可能吗?如果没有,有没有其他不那么复杂的替代方法?

【问题讨论】:

标签: javascript asp.net google-maps


【解决方案1】:

这是你的做法:

http://jsfiddle.net/y7pdb5b2/

// Here we set the address
var address = "oxford street london";


// We call get location, the callback will return the address;
// You need to add some error checking to ensure you get something back;

GetLocation(address, function (coord) {
    alert(coord);
    var lat = coord.lat,
        lng = coord.lng;

});


function GetLocation(address, callback) {
    var geocoder = new google.maps.Geocoder();
    geocoder.geocode({
        'address': address
    }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
        callback(results[0].geometry.location);
    } else {
        callback();
    }

    })
};

【讨论】:

  • 我是 javascript 的新手,所以上面的代码对我来说没有多大意义。我如何将地址传递给 var 地址,因为它会根据数据库不断变化。它不是一个特定的地址。
  • 而且我仍然不知道如何将纬度和经度值传递给标签,它仍在使用我不想要的 txtAddress。
  • 是代码中的小错误,您将地址作为标签(var)地址传递给函数
  • 当你说一个标签时,你是指 HTML dom 中的一个标签,还是你指的标签是一个变量。
  • 我已经编辑了整个代码。即使这不起作用。任何帮助将不胜感激。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-06
  • 2019-04-29
  • 1970-01-01
  • 2017-07-08
相关资源
最近更新 更多