我正在开发一个应用程序,它将检测用户位置显示的纬度和经度,并显示针对它们的物理地址。当我运行它时,我什么也没得到。
我的系统位置已打开,并且具有有效的Internet连接。我每次在任务栏中看到有人跟踪位置的点时,仍然看不到任何东西。
这是我的代码。
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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
您可以尝试使用以下方法查找位置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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6996 次 |
| 最近记录: |