Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use HTML5 geolocation in C# application

Tags:

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 application.

For Internet Explorer 9+, there is a registry key that you can add urls to allow a url without needing user verification. If you add an REG_DWORDvalue named domain.com under HKCU\Software\Microsoft\Internet Explorer\Geolocation\HostConsent path browser will allow geolocation request automatically. However I can't run Internet Explorer hidden so thats not working for me since thief shouldn't realize and see what's going on.

  • I need to run Internet Explorer hidden somehow
  • ...or I need to embed webkit or something to my application but I don't know how can I use it or how can I allow this request programmatically.

I prefer second way because Internet Explorer is now terminated by Microsoft and I think next version will have different structure.

How can I embed and use Webkit or GeckoFX to my application? How can I allow a geolocation request programmatically in this application?

like image 279
Batuhan Avatar asked Mar 23 '15 13:03

Batuhan


People also ask

What is HTML5 geolocation how do you use it?

HTML5 Geolocation API lets you share your location with your favorite web sites. A JavaScript can capture your latitude and longitude and can be sent to backend web server and do fancy location-aware things like finding local businesses or showing your location on a map.

Does HTML5 have geolocation?

HTML5 geolocation lets you find the geographic coordinates of a website visitor's current location like the longitude and the latitude. The information provided by HTML 5 geolocation can be used to optimise the browsing experience.

Is HTML Geolocation API free?

So yes, getting a user's lat/lon is 'free'--no need to pay for that feature by itself.

How do I connect to geolocation?

Open the Google Chrome app. Touch the Chrome menu . Touch Settings > Site settings > Location. Use the switch to either have Chrome ask before accessing your location, or to block all sites from accessing your location.


2 Answers

Relying on a hidden browser is a risky solution, and it will inevitably break at some point in the future.

Instead you want to build geolocation functionality into your own application. The two major sources of location info are your IP address (which you then feed into any of the GeoIP providers) and cellular/Wi-Fi stations visible (which you feed into Google geolocation API).

like image 198
Michael Diomin Avatar answered Sep 19 '22 00:09

Michael Diomin


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.

like image 43
Cyril Durand Avatar answered Sep 19 '22 00:09

Cyril Durand