如何在C#中启用位置服务?

时间:2013-12-27 20:37:12

标签: c# geolocation console-application geocoding

我想创建一个简单的控制台应用程序,它将检索计算机的坐标。我真的不知道如何在计算机上启用位置服务.. 到目前为止,我的代码看起来像这样:

GeoCoordinateWatcher watcher = new GeoCoordinateWatcher();
watcher.TryStart(false, TimeSpan.FromSeconds(10));
GeoCoordinate CurrentCoordinate = watcher.Position.Location;
if (CurrentCoordinate.IsUnknown)
{
    Console.WriteLine("At this time, your coordinates are unknown..\n");
    Console.WriteLine("Do you want to force the program to find your coordinates?\n(Y = yes, All Any Other Key = no)");
    ConsoleKeyInfo KPress = Console.ReadKey(false);
    if (KPress.Key == ConsoleKey.Y)
    {
        /*
        Enabling The Location Services & Find The User's Location Coordinates
        */

        GeoCoordinate NewCoord = ReFindCoordinates(watcher);

        Console.WriteLine("Latitude: " + NewCoord.Latitude);
        Console.WriteLine("Longtitude: " + NewCoord.Longitude);
    }
    else
    {
        Environment.Exit(0);
    }
}
else
{
    Console.WriteLine("Latitude: " + CurrentCoordinate.Latitude);
    Console.WriteLine("Longtitude: " + CurrentCoordinate.Longitude);
}

有人知道如何通过C#启用此服务吗?我很乐意得到一些帮助.. 谢谢。

3 个答案:

答案 0 :(得分:0)

从网上抓取地理位置?

internal GeoLoc GetMyGeoLocation()
{
try
{
    //create a request to geoiptool.com
    var request = WebRequest.Create(new Uri("http://geoiptool.com/data.php")) as HttpWebRequest;


if (request != null)
{
    //set the request user agent
    request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727)";

    //get the response
    using (var webResponse = (request.GetResponse() as HttpWebResponse))
        if (webResponse != null)
            using (var reader = new StreamReader(webResponse.GetResponseStream()))
            {

                //get the XML document
                var doc = new XmlDocument();
                doc.Load(reader);

                //now we parse the XML document
                var nodes = doc.GetElementsByTagName("marker");

                Guard.AssertCondition(nodes.Count > 0,"nodes",new object());
                //make sure we have nodes before looping
                //if (nodes.Count > 0)
                //{
                    //grab the first response
                    var marker = nodes[0] as XmlElement;

                    Guard.AssertNotNull(marker, "marker");

                    var _geoLoc = new GeoLoc();
                    //get the data and return it
                    _geoLoc.City = marker.GetAttribute("city");
                    _geoLoc.Country = marker.GetAttribute("country");
                    _geoLoc.Code = marker.GetAttribute("code");
                    _geoLoc.Host = marker.GetAttribute("host");
                    _geoLoc.Ip = marker.GetAttribute("ip");
                    _geoLoc.Latitude = marker.GetAttribute("lat");
                    _geoLoc.Lognitude = marker.GetAttribute("lng");
                    _geoLoc.State = GetMyState(_geoLoc.Latitude, _geoLoc.Lognitude);

                    return _geoLoc;
                //}
            }
       }

    // this code would only be reached if something went wrong 
    // no "marker" node perhaps?
    return new GeoLoc();
}
catch (Exception ex)
{
    throw;
}
}

Get IP geolocation using C# & WPF

答案 1 :(得分:0)

为什么不直接使用Bing Maps API并从他们的网络服务获取您的位置? http://msdn.microsoft.com/en-us/library/ff701713.aspx

答案 2 :(得分:0)

使用GeoCoordinateWatcher课程时,检索位置会有一些延迟。因此,您可以挂钩 watcher_PositionChanged 事件并从那里获取位置。希望这有帮助

 _watcher = new GeoCoordinateWatcher();
 _watcher.TryStart(true, TimeSpan.FromMilliseconds(1000));
 _watcher.PositionChanged += watcher_PositionChanged;

 void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
    {
        if (e.Position.Location != null)
        {
            Console.WriteLine("Lat: {0}, Long: {1}",
                e.Position.Location.Latitude,
                e.Position.Location.Longitude);
            _watcher.Stop();
        }
    }