How can I use HTML5 geolocation in C# application

前端 未结 4 1489
情书的邮戳
情书的邮戳 2021-02-05 08:05

I\'m developing an anti-theft software to get computers exact location. Notebooks with built-in gps are very rare in my country so I have to use HTML5 Geolocation in my applicat

4条回答
  •  醉酒成梦
    2021-02-05 08:45

    Have a look at the GeoCoordinateWatcher class which is defined in the System.Device assembly

    The GeoCoordinateWatcher class supplies coordinate-based location data from the current location provider. The current location provider is prioritized as the highest on the computer, based on a number of factors, such as the age and accuracy of the data from all providers, the accuracy requested by location applications, and the power consumption and performance impact associated with the location provider. The current location provider might change over time, for instance, when a GPS device loses its satellite signal indoors and a Wi-Fi triangulation provider becomes the most accurate provider on the computer.

    Usage example :

    static void Main(string[] args)
    {
        GeoCoordinateWatcher watcher = new GeoCoordinateWatcher();
    
        watcher.StatusChanged += (sender, e) =>
        {
            Console.WriteLine("new Status : {0}", e.Status);
        };
    
        watcher.PositionChanged += (sender, e) =>
        {
            Console.WriteLine("position changed. Location : {0}, Timestamp : {1}",
                e.Position.Location, e.Position.Timestamp);
        };
    
        if(!watcher.TryStart(false, TimeSpan.FromMilliseconds(5000)))
        {
             throw new Exception("Can't access location"); 
        }
    
        Console.ReadLine();
    }
    

    I think this class relies on the same mechanism than the one use by Internet Explorer.

    When you will use it, you will have an icon in your system notification tray telling that location has recently been accessed.

    enter image description here

    You will also have an entry added on windows logs.

提交回复
热议问题