如何在C#应用程序中使用HTML5地理定位

时间:2015-03-23 13:35:19

标签: c# html5 geolocation w3c-geolocation

我正在开发一种防盗软件,以确保计算机的准确位置。内置gps的笔记本在我的国家非常罕见,所以我必须在我的应用程序中使用HTML5 Geolocation

对于Internet Explorer 9+,有一个注册表项,您可以添加网址以允许网址而无需用户验证。如果您在REG_DWORD路径浏览器下添加名为domain.com的HKCU\Software\Microsoft\Internet Explorer\Geolocation\HostConsent值,则会自动允许地理定位请求。但是我无法隐藏Internet Explorer,因为小偷不应该意识到并且看到发生了什么,因此不能为我工作。

  • 我需要以某种方式隐藏Internet Explorer
  • ...或者我需要将webkit或其他内容嵌入到我的应用程序中,但我不知道如何使用它或如何以编程方式允许此请求。

我更喜欢第二种方式,因为Internet Explorer现在已被Microsoft终止,我认为下一版本将具有不同的结构。

如何在我的应用程序中嵌入和使用Webkit或GeckoFX?如何在此应用程序中以编程方式允许地理定位请求?

4 个答案:

答案 0 :(得分:8)

依赖隐藏的浏览器是一个有风险的解决方案,它将在未来的某个时刻不可避免地破裂。

相反,您希望在自己的应用程序中构建地理定位功能。位置信息的两个主要来源是您的IP地址(然后您可以将其输入任何GeoIP providers)和可见的蜂窝/ Wi-Fi电台(您可以将其输入Google geolocation API)。

答案 1 :(得分:4)

查看System.Device程序集

中定义的GeoCoordinateWatcher
  

GeoCoordinateWatcher类提供来自当前位置提供程序的基于坐标的位置数据。根据许多因素,当前位置提供商优先考虑计算机上的最高位置,例如所有提供商的数据的年龄和准确性,位置应用程序请求的准确性以及与之相关的功耗和性能影响。位置提供者。当前位置提供商可能会随着时间的推移而发生变化,例如,当GPS设备在室内丢失其卫星信号并且Wi-Fi三角测量提供商成为计算机上最准确的提供商时。

用法示例:

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();
}

我认为这个类依赖于与Internet Explorer使用的机制相同的机制。

当您使用它时,系统通知栏中会有一个图标,告知最近访问过该位置。

enter image description here

您还将在Windows日志中添加一个条目。

答案 2 :(得分:2)

如果要部署到现代版本的Windows,则可以使用.NET System.Device.Location本机库,它允许您获取设备位置信息。

以下是MSDN上有关如何使用它的链接 GeoCoordinate Class

如果使用XAML,您也可以尝试使用此方法。 Detect Users Location using XAML

答案 3 :(得分:1)

您可以使用PhantomJS将webkit浏览器嵌入到您的应用程序中。 PhantomJS是一个无头浏览器,可以通过搜索NuGet或NuGet命令行PM> Install-Package PhantomJS添加到您的应用程序中。一旦PhantomJS添加到您的项目中,您将需要构建一个文件来控制幻像,如:

 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");
            }
        }

然后你会想要建立一个控制文件来告诉phantomjs去哪里;就像是:   

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