How can I use HTML5 geolocation in C# application

前端 未结 4 1426
情书的邮戳
情书的邮戳 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:34

    If you are deploying to a modern version of Windows, you could use the library native to .NET System.Device.Location which allows you to obtain device location information.

    Here is a link on MSDN for how to use it GeoCoordinate Class

    If using XAML, you can also try this method. Detect Users Location using XAML

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-05 08:46

    You can embed a webkit browser into your application by using PhantomJS. PhantomJS is a headless browser and can be added to your application through searching NuGet or the NuGet command line PM> Install-Package PhantomJS. Once PhantomJS has been added to your project, you'll want to build a file to control phantom something like:

     public string PhantomJson(string phantomControlFile, params string[] arguments)
            {
                string returnJsonString = String.Empty;
    
                if (!String.IsNullOrEmpty(URL))
                {
                    ProcessStartInfo startInfo = new ProcessStartInfo
                    {
                        CreateNoWindow = true,
                        RedirectStandardError = true,
                        RedirectStandardOutput = true,
                        FileName = Path.Combine(PhantomExecutionPath, "phantomjs.exe"),
                        UseShellExecute = false,
                        WorkingDirectory = PhantomExecutionPath,
                        Arguments = @"--proxy-type=none --ignore-ssl-errors=true {1} ""{0}"" {2}".FormatWith(URL, phantomControlFile, 
                            arguments.Any() ? String.Join(" ", arguments) : String.Empty)
                    };
    
                    StringBuilder receivedData = new StringBuilder();
                    using (Process p = Process.Start(startInfo))
                    {
                        p.OutputDataReceived += (o, e) =>
                        {
                            if (e.Data != null && e.Data != "failed")
                            {
                                //returnJsonString = e.Data;
                                receivedData.AppendLine(e.Data);
                            }
                        };
                        p.BeginOutputReadLine();
                        p.WaitForExit();
                    }
                    returnJsonString = receivedData.ToString();
    
                    if (!String.IsNullOrEmpty(returnJsonString))
                    {
                        return returnJsonString;
                    }
                    else
                    {
                        throw new ArgumentNullException("Value returned null. Unable to retrieve data from server");
                    }
                }
                else
                {
                    throw new ArgumentNullException("Url cannot be null");
                }
            }
    

    Then you'll want to build a control file to tell phantomjs where to go; something like:

    var args, myurl, page, phantomExit, renderPage, system;
    
    system = require("system");
    args = system.args;
    page = null;
    myurl = args[1];
    
    phantomExit = function(exitCode) { // this is needed as there are time out issues when it tries to exit.
      if (page) {
        page.close();
      }
      return setTimeout(function() {
        return phantom.exit(exitCode);
      }, 0);
    };
    
    renderPage = function(url) {
      page = require("webpage").create();
        return page.open(url, function(status) {
          if (status === 'success') {
             // Process Page and console.log out the values
             return phatomExit(0);
          } else {
             console.log("failed");
             return phantomExit(1);
          }
         });
       };
      renderPage(myurl);
    
    0 讨论(0)
  • 2021-02-05 08:51

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

    0 讨论(0)
提交回复
热议问题