首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >想要在c#代码中获得GPS坐标吗?

想要在c#代码中获得GPS坐标吗?
EN

Stack Overflow用户
提问于 2013-11-22 22:46:28
回答 1查看 3.2K关注 0票数 0

我在GPS坐标上有点问题。我可以使用地理位置获得asp.net / javascript中的坐标,但需要这些坐标在c#代码中的方法中可用。不幸的是,由于某种原因,检索到的坐标不是,即使我将它们放入标签中(由于某种原因,它们永远不会出现在标签中)。

所以,我现在想的是尝试以某种方式将坐标(只需要纬度和经度)直接输入到c#中,即使我必须通过c#运行一些javascript (不确定如何做到这一点)。

有谁有什么想法吗?我已经发布了下面的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>
EN

回答 1

Stack Overflow用户

发布于 2014-02-04 06:43:54

我猜您想要在codebehind方法中使用坐标?

为什么不在打开页面时运行,然后单击按钮,从标签/文本框中检索数据?

代码语言:javascript
复制
<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的文本框将获得您可以使用的坐标。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20147631

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档