简体   繁体   English

地理位置-如何在文本框中显示经纬度

[英]Geolocation - how to display lat/lon in a textbox

Having some weird problems here, and I'm completely unsure what it is. 这里有一些奇怪的问题,我完全不确定这是什么。

Basically I've got two textboxes, lblLat and lblLon. 基本上,我有两个文本框,lblLat和lblLon。 I want these to display the latitude and longitude, retrieved using a geolocation. 我希望它们显示使用地理位置检索的纬度和经度。 Code for all this is: 所有这些的代码是:

<asp:Textbox ID="lblLat" runat="server" BorderColor="White" BorderStyle="None" ForeColor="Black" Width="50px">Unknown</asp:Textbox>
        <asp:Textbox ID="lblLon" runat="server" BorderColor="White" BorderStyle="None" ForeColor="Black" Width="50px">Unknown</asp:Textbox>

<button id="btnLocate" onclick="GetLocation()" style="width: 15%">Loc</button>

        <script type="text/javascript">

            function GetLocation()
            {
                if (navigator.geolocation)
                {
                    navigator.geolocation.getCurrentPosition(ShowPosition, ShowError, { maximumAge: 5000, timeout: 45000 });
                }
                else { alert("Geolocation is not supported by this browser."); }
            }
            function ShowPosition(position)
            {
                //document.getElementById('<%=lblLat.ClientID %>').value = position.coords.latitude;
                //document.getElementById('<%=lblLon.ClientID %>').value = position.coords.longitude;
                var latlondata = position.coords.latitude + "," + position.coords.longitude;
                alert(latlondata);
                document.getElementById('<%=lblLat.ClientID %>').value = "Me";
                document.getElementById('<%=lblLon.ClientID %>').value = "Me";
            }
            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>

Now, here I've got it so that the latitude and longitude is retrieved and displayed in the latlondata, and it's subsequent alert. 现在,这里已经有了它,以便检索纬度和经度并将其显示在latlondata中,这是随后的警报。

However, I can't get the two labels to show the location. 但是,我无法获得两个标签来显示位置。 Obviously in the example above they show "Me" (which they do, but only EVENTUALLY) but even with the "Me" lines not there and the lines setting them to the latitude and longitude not commented out, they still read their initial value of "unknown". 显然,在上面的示例中,它们显示“ Me”(但实际上只是显示),但是即使不存在“ Me”行,并且未将其设置为纬度和经度的行,它们仍会读取其初始值“未知”。

may try using 可以尝试使用
document.getElementById('<%=lblLat.ClientID %>'). document.getElementById('<%= lblLat.ClientID%>')。 innerHTML instead of innerHTML代替
document.getElementById('<%=lblLat.ClientID %>').value document.getElementById('<%= lblLat.ClientID%>')。value

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM