如何使用C#获取当前位置的经度和纬度

har*_*ini 2 c#

我是C#的新手,我已经尝试获取当前位置的纬度和经度。以下是我尝试的代码。

public string GetLocationProperty()
{
    double a = 0.0;
    string b = "";
    GeoCoordinateWatcher watcher = new GeoCoordinateWatcher();

    // Do not suppress prompt, and wait 1000 milliseconds to start.
    watcher.TryStart(false, TimeSpan.FromMilliseconds(1000));

    GeoCoordinate coord = watcher.Position.Location;

    if (coord.IsUnknown != true)
    {
        //Console.WriteLine("Lat: {0}, Long: {1}",
        //    coord.Latitude,
        //    coord.Longitude);
        a = coord.Latitude;
        b = a.ToString();
    }
    else
    {
         Console.WriteLine("Unknown latitude and longitude.");
    }
    return b;
}
Run Code Online (Sandbox Code Playgroud)

Abh*_*hek 5

干得好

GeoCoordinateWatcher watcher= new GeoCoordinateWatcher(GeoPositionAccuracy.Default);
watcher.Start(); //started watcher
GeoCoordinate coord = watcher.Position.Location;
if (!watcher.Position.Location.IsUnknown)
{
    double lat = coord.Latitude; //latitude
    double long = coord.Longitude;  //logitude
}
Run Code Online (Sandbox Code Playgroud)