我试图展示并获取网站访问者的坐标'将它存储在我的数据库中的位置,我已经在javascript中使用google maps API进行了简单的试用。 现在我需要将坐标值从javascript传递给我的代码以进行所需的计算并将其存储在数据库中。
这里是定位代码
<script>
function success(position) {
var s = document.querySelector('#status');
if (s.className == 'success') {
return;
}
s.innerHTML = "Gotcha!";
s.className = 'success';
var mapcanvas = document.createElement('div');
mapcanvas.id = 'mapcanvas';
mapcanvas.style.height = '400px';
mapcanvas.style.width = '560px';
document.querySelector('article').appendChild(mapcanvas);
var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
var myOptions = {
zoom: 15,
center: latlng,
mapTypeControl: false,
navigationControlOptions: { style: google.maps.NavigationControlStyle.SMALL },
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("mapcanvas"), myOptions);
var marker = new google.maps.Marker({
position: latlng,
map: map,
title: "You are here! (at least within a " + position.coords.accuracy + " meter radius)"
});
}
function error(msg) {
var s = document.querySelector('#status');
s.innerHTML = typeof msg == 'string' ? msg : "failed";
s.className = 'fail';
}
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(success, error);
} else {
error('not supported');
}
//the problem is here!
var getter = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
document.getElementById('<%=send_laty.ClientID%>') = getter.lat;
document.getElementById('<%=send_lony.ClientID%>') = getter.lng;
alert(getter)
</script>
我创建了2个隐藏字段,用于将变量从javascript传递到代码
<asp:HiddenField ID="send_laty" runat="server" Value="" />
<asp:HiddenField ID="send_lony" runat="server" Value="" />
我正在aspx.vb页面的page_load上测试它们
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim output As String
output = send_laty.Value + " ___ " + send_lony.Value
MsgBox(output)
End Sub
但是输出是_______,这意味着变量没有传递!
我需要提示一下为什么它没有传递价值!
提前致谢,
答案 0 :(得分:0)
您需要将结果分配给value
属性。
document.getElementById('<%=send_laty.ClientID%>').value = getter.lat;
document.getElementById('<%=send_lony.ClientID%>').value = getter.lng;
答案 1 :(得分:0)
aspx.vb页面的Page_Load在javascript之前执行,因此值始终为空。您需要回发值以便将数据发送回服务器
而不是使用
dim x as string = send_lony.Value
和
的document.getElementById(&#39;&LT;%= send_laty.ClientID%GT;&#39)
我用过
dim x as string = Request.Form("send_lony")
与
document.getElementById('<%=send_laty.ClientID%>').value
我感谢你的帮助:)。
答案 2 :(得分:0)
要使用动态控制将值从javascript传递到页面后面,请执行以下操作:
In aspx page header content,
<script language="javascript" type="text/javascript">
function executedelete(txt) {
alert(txt);
document.getElementById('<%=ctlHiddenField.ClientID%>').value = txt
__doPostBack("<%= deletetherecord.UniqueID %>", "");
}
</script>
在aspx页面正文内容中,
<asp:Button ID="deletetherecord" runat="server" style="display:none" />
<asp:HiddenField ID="ctlHiddenField" runat="server" />
在Page Behind中,
Protected Sub deletetherecord_Click(sender As Object, e As System.EventArgs) Handles deletetherecord.Click
Dim txt As String = ctlHiddenField.Value
'do processing here
End Sub
在Page Behind功能中,创建动态控制
SomeLabel.Text &= "<input id=""deletebutton"" type=""button"" value=""Delete"" onClick=""executedelete('" & gv.Rows(row).Cells(5).Text & "');"" />"