我在GPS坐标上有点问题。我可以使用地理位置获得asp.net / javascript中的坐标,但需要这些坐标在c#代码中的方法中可用。不幸的是,由于某种原因,检索到的坐标不是,即使我将它们放入标签中(由于某种原因,它们永远不会出现在标签中)。
所以,我现在想的是尝试以某种方式将坐标(只需要纬度和经度)直接输入到c#中,即使我必须通过c#运行一些javascript (不确定如何做到这一点)。
有谁有什么想法吗?我已经发布了下面的javascript:
<button id="btnLocate" runat="server" onclick="GetLocation()" style="width: 15%">Loc</button>
<script type="text/javascript">
function GetLocation()
{
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(ShowPosition, ShowError, { maximumAge: 5000, timeout: 10000 });
}
else { alert("Geolocation is not supported by this browser."); }
}
function ShowPosition(position)
{
var latdata = position.coords.latitude;
var londata = position.coords.longitude;
document.getElementById("lblLat").value = latdata;
document.getElementById("lblLon").value = londata;
}
function ShowError(error)
{
if (error.code == 1)
{
alert("User denied the request for Geolocation.");
}
else if (error.code == 2)
{
alert("Location information is unavailable.");
}
else if (error.code == 3)
{
alert("The request to get user location timed out.");
}
else
{
alert("An unknown error occurred.");
}
}
</script>发布于 2014-02-04 06:43:54
我猜您想要在codebehind方法中使用坐标?
为什么不在打开页面时运行,然后单击按钮,从标签/文本框中检索数据?
<script type="text/javascript" id="getCord">
if(typeof navigator.geolocation === 'undefined')
{
alert("Geolocation services are not supported by your web browser");
}
else
{
navigator.geolocation.getCurrentPosition(handleLocation, handleError);
}
function handleLocation(position)
{
var lat = position.coords.latitude;
document.getElementById('<%= latTextBox.ClientID %>').value = lat;
var lon = position.coords.longitude;
document.getElementById('<%= lonTextBox.ClientID %>').value = lon;
}
function handleError(error)
{
switch (error.code)
{
case error.TIMEOUT:
alert('Timeout');
break;
case error.POSITION_UNAVAILABLE:
alert('Position unavailable');
break;
case error.PERMISSION_DENIED:
alert('Permission denied');
break;
case error.UNKNOWN_ERROR:
alert('Unknown error');
break;
}
}
这将在不按任何按钮的情况下运行,并且您在lonTextBox上ID为latTextBox的文本框将获得您可以使用的坐标。
https://stackoverflow.com/questions/20147631
复制相似问题