繁体   English   中英

C#在asp.net应用程序中获取当前纬度和经度

[英]C# Get current latitude and longitude in asp.net application

我正在开发一个应用程序,它将检测用户位置,显示纬度和经度,并针对它们显示物理地址。 当我运行这个时,我什么也没得到。

我的系统位置已打开并且具有活动的互联网连接。 我每次都在任务栏中看到一个点,表明有人正在跟踪位置,但我仍然一无所获。

这是我的代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Device.Location;
using System.Diagnostics;
namespace APIsProject
{
public partial class GetLatitudeandLongitude : System.Web.UI.Page
{
    private string latitude;
    private string longitute;
    private GeoCoordinateWatcher watcher = new GeoCoordinateWatcher();
    protected void Page_Load(object sender, EventArgs e)
    {
        GeoCoordinateWatcher watcher = new GeoCoordinateWatcher();
        watcher = new GeoCoordinateWatcher();
        // Catch the StatusChanged event.  
        watcher.StatusChanged += Watcher_StatusChanged;
        // Start the watcher.  
        watcher.Start();

    }

    private void Watcher_StatusChanged(object sender, 
    GeoPositionStatusChangedEventArgs e) // Find GeoLocation of Device  
    {
        try
        {
            if (e.Status == GeoPositionStatus.Ready)
            {
                // Display the latitude and longitude.  
                if (watcher.Position.Location.IsUnknown)
                {
                    latitude = "0";
                    longitute = "0";
                }
                else
                {
                    latitude = 
       watcher.Position.Location.Latitude.ToString();
                    longitute = 
       watcher.Position.Location.Longitude.ToString();
                }
            }
            else
            {
                latitude = "0";
                longitute = "0";
            }
        }
        catch (Exception)
        {
            latitude = "0";
            longitute = "0";
        }
    }

    protected void InsertButton_Click(object sender, EventArgs e)
    {
         TextBoxLatitude.Text = latitude;
        TextBoxLongitude.Text = longitute;

    }
 }
}

您可以尝试使用navigator.geolocation.getCurrentPosition()来查找位置:

<html>
<body>

<p>Click the button to get your coordinates.</p>

<button onclick="getLocation()">Try It</button>

<p id="demo"></p>

<script>
var x = document.getElementById("demo");

function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition);
    } else { 
        x.innerHTML = "Geolocation is not supported by this browser.";
    }
}

function showPosition(position) {
    x.innerHTML = "Latitude: " + position.coords.latitude + 
    "<br>Longitude: " + position.coords.longitude;
}
</script>

</body>
</html>

现场演示

简单试试这个..它的工作...

 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="location.WebForm2" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script> <script> function getLocation() { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(showPosition); } else { document.getElementById("Label1").value = "Geolocation is not supported by this browser."; } } function showPosition(position) { document.getElementById("TextBox1").value = position.coords.latitude document.getElementById("TextBox2").value = position.coords.longitude; } </script> <%--<button onclick="getLocation()">Try It</button>--%> <form id="form1" runat="server"> <div> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="getLocation();return false;" /> <asp:Label ID="Label1" runat="server" Text=""></asp:Label> </div> </form> </body> </html>

暂无
暂无

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

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