Geo location is available only for smartphone applications, is there any third-party library for defining or Web API for locating the application?

  • It seems, according to the documentation, Geolocator is also available in the desktop msdn.microsoft.com/en-us/library/windows/apps/… applications - hardsky

2 answers 2

using System.Device.Location; class Geo { public double x, y, z; public Geo() { GeoCoordinateWatcher watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High); watcher.MovementThreshold = 1.0; watcher.TryStart(false, TimeSpan.FromMinutes(1.0)); Thread.Sleep(100); if (watcher.Position.Location.IsUnknown == false) { GeoCoordinate coor = watcher.Position.Location; x = coor.Latitude; y = coor.Longitude; z = coor.Altitude; } } } 

I tried to write my class easier, everything works

    This code works even if there is no access to GPS data, it determines by IP address, data from Wi-Fi.

     public class ImmediateLocation : IDisposable { private GeoCoordinateWatcher _watcher; private Action<GeoCoordinate> _action; public ImmediateLocation(Action<GeoCoordinate> a) { Debug.Assert(a != null); _action = a; } public void GetLocation() { if (_watcher == null) { _watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.Default); _watcher.MovementThreshold = 1000; _watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>> (_watcher_PositionChanged); _watcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs> (_watcher_StatusChanged); _watcher.Start(false); if (_watcher.Status == GeoPositionStatus.Disabled || _watcher.Permission == GeoPositionPermission.Denied) Dispose(); } } void _watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e) { if (e.Status == GeoPositionStatus.Disabled || _watcher.Permission == GeoPositionPermission.Denied) Dispose(); } void _watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e) { _action(e.Position.Location); Dispose(); } public void Dispose() { if (_watcher != null) { _watcher.Stop(); _watcher.PositionChanged -= new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>> (_watcher_PositionChanged); _watcher.StatusChanged -= new EventHandler<GeoPositionStatusChangedEventArgs> (_watcher_StatusChanged); _watcher.Dispose(); } _watcher = null; _action = null; } } 

    Example of use:

     var immediate = new ImmediateLocation(x => location = x); immediate.GetLocation(); GeoCoordinate location; 

    If you don't need high accuracy, you can use a web service, for example:

     public class IpProperties { public string Status { get; set; } public string Country { get; set; } public string CountryCode { get; set; } public string Region { get; set; } public string RegionName { get; set; } public string City { get; set; } public string Zip { get; set; } public string Lat { get; set; } public string Lon { get; set; } public string TimeZone { get; set; } public string ISP { get; set; } public string ORG { get; set; } public string AS { get; set; } public string Query { get; set; } } public string IPRequestHelper(string url) { HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(url); HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse(); StreamReader responseStream = new StreamReader(objResponse.GetResponseStream()); string responseRead = responseStream.ReadToEnd(); responseStream.Close(); responseStream.Dispose(); return responseRead; } public IpProperties GetCountryByIP(string ipAddress) { string ipResponse = IPRequestHelper("http://ip-api.com/xml/" + ipAddress); using (TextReader sr = new StringReader(ipResponse)) { using (System.Data.DataSet dataBase = new System.Data.DataSet()) { IpProperties ipProperties = new IpProperties(); dataBase.ReadXml(sr); ipProperties.Status = dataBase.Tables[0].Rows[0][0].ToString(); ipProperties.Country = dataBase.Tables[0].Rows[0][1].ToString(); ipProperties.CountryCode = dataBase.Tables[0].Rows[0][2].ToString(); ipProperties.Region = dataBase.Tables[0].Rows[0][3].ToString(); ipProperties.RegionName = dataBase.Tables[0].Rows[0][4].ToString(); ipProperties.City = dataBase.Tables[0].Rows[0][5].ToString(); ipProperties.Zip = dataBase.Tables[0].Rows[0][6].ToString(); ipProperties.Lat = dataBase.Tables[0].Rows[0][7].ToString(); ipProperties.Lon = dataBase.Tables[0].Rows[0][8].ToString(); ipProperties.TimeZone = dataBase.Tables[0].Rows[0][9].ToString(); ipProperties.ISP = dataBase.Tables[0].Rows[0][10].ToString(); ipProperties.ORG = dataBase.Tables[0].Rows[0][11].ToString(); ipProperties.AS = dataBase.Tables[0].Rows[0][12].ToString(); ipProperties.Query = dataBase.Tables[0].Rows[0][13].ToString(); return ipProperties; } } } 

    If you need this info for a web resource, you can do it on js'e like this:

     function getLocation() { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(showPosition); } else { console.log("Geolocation is not supported by this browser."); } } function showPosition(position) { console.log("Latitude: " + position.coords.latitude + "<br>Longitude: " + position.coords.longitude); } 
    • variable named coord constantly matters Uknown - Alexey
    • @ Alexey I updated the example. Try it, at the same time pay attention to the statuses. - Mstislav Pavlov
    • I have just copied your example and the status does not change uknown - Alexey