c# - Cordova - 根据地理位置检测用户国家

标签 c# asp.net cordova geolocation

我已经在我的 cordova 应用程序中实现了 cordova-plugin-geolocation 插件。我只是想在不需要第三方 API 的情况下获取用户所在的国家/地区。我的应用程序正在与用 C# 编写的 WebApi 进行通信,所以我不介意是否有针对此的 C# 解决方案。我宁愿不探索使用包括 Google 在内的第三方工具,但如果这是最佳选择,那么我会探索。

在准确性方面,我只需要检测他们是否在英国以外。

最佳答案

还有另一种可能性,它根本不需要任何第三方 API 和互联网连接。

此答案基于 point-in-polygon-aglorithm .

您需要一个国家/地区作为由纬度/经度点组成的多边形,您可以使用 GGIS-Editor 获取奥地利的所有纬度/经度点(例如)和来自 here 的相关数据(作为 shapefile)或来自自然地球(选择管理员 0 – 国家)here

  1. 安装 GGIS 编辑器
  2. 解压缩 AUT_adm_shp.zip
  3. 启动GGIS-Editor
  4. 选择选项卡:层 -> 添加层 -> 添加矢量层
  5. 选择 AUT_adm0.shp (shapefile),它现在应该如下所示: enter image description here

  6. 通过标记工具(带光标的黄色按钮)标记整个多边形: enter image description here

  7. 选择选项卡:图层 -> 另存为

  8. 另存为 GeoJSON 文件
  9. 从这个 GeoJSON 文件中复制所有 lng/lat 坐标: enter image description here

  10. 当您以这种格式将所有 Lng/Lat-Points 复制为数组元素时:

    [ [ 经度, 纬度 ], .....]


然后你可以使用我的 fiddle 代码:

var countryLngLatsOfAustria = [ [ lng, lat ], .....];

function pointInPoly(verties, testx, testy) {
  var i,
  j,
  c = 0,
  nvert = verties.length;
  for (i = 0, j = nvert - 1; i < nvert; j = i++) {
    if (((verties[i][0] > testy) != (verties[j][0] > testy)) && (testx < (verties[j][1] - verties[i][1]) * (testy - verties[i][0]) / (verties[j][0] - verties[i][0]) + verties[i][1]))
    c = !c;
  }
  return c;
}

// use current location
navigator.geolocation.getCurrentPosition(
  function (position) {
    alert(position.coords.longitude +", "+ position.coords.latitude);
    var isInAustria = pointInPoly(countryLngLatsOfAustria, position.coords.latitude, position.coords.longitude);
    if (isInAustria) alert('User is located within austria!');
    else alert('User is located outside from austria!');
  }, 
  function (err) {
    console.log(err);
  }, 
  {
    timeout: 5000,
    enableHighAccuracy: true
  }
);

(链接:https://jsfiddle.net/pfa028n8)了解您是位于奥地利境内还是境外。当然,它还与其他国家或“联合国家”合作,例如查明用户是否在欧洲境内。

希望这对您有所帮助。

关于c# - Cordova - 根据地理位置检测用户国家,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41072502/

相关文章:

asp.net - 如何在 ASP.NET 的服务器端获取输入值(输入文本)?

c# - 在 MVC 中使用数据库连接包装方法

javascript - 在 Cordova iOS 中使用 Papaparse 解析本地 CSV 文件

c# - 在使用库方面,C/C++/Objective-C 与 C# 相比如何?

C# 创建和使用函数

c# - 检查本地用户所属的组

asp.net - CustomValidator 在客户端上触发,但验证失败时验证控件不会呈现

ios - 我不小心从我的 xcode 项目中删除了一个文件夹,它去了哪里?

ios - 如何刷新 xcode/cordova 中的 www 目录

c# - Monogame、SharpDX 和 XNA 有什么关系?