[英]Want to get GPS coordinates in c# codebehind
我的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>
我猜您想在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的文本框将获得可使用的坐标。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.