我试图显示并获取网站访问者位置的坐标,以将其存储在我的数据库中,我已经在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
但是输出是_______,这意味着未传递变量!
我需要一个提示,为什么它没有传递值!
提前致谢,
您需要将结果分配给value
属性。
document.getElementById('<%=send_laty.ClientID%>').value = getter.lat;
document.getElementById('<%=send_lony.ClientID%>').value = getter.lng;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句