我已经在我的 cordova 应用程序中实现了 cordova-plugin-geolocation 插件。我只是想在不需要第三方 API 的情况下获取用户所在的国家/地区。我的应用程序正在与用 C# 编写的 WebApi 进行通信,所以我不介意是否有针对此的 C# 解决方案。我宁愿不探索使用包括 Google 在内的第三方工具,但如果这是最佳选择,那么我会探索。
在准确性方面,我只需要检测他们是否在英国以外。
最佳答案
还有另一种可能性,它根本不需要任何第三方 API 和互联网连接。
此答案基于 point-in-polygon-aglorithm .
您需要一个国家/地区作为由纬度/经度点组成的多边形,您可以使用 GGIS-Editor 获取奥地利的所有纬度/经度点(例如)和来自 here 的相关数据(作为 shapefile)或来自自然地球(选择管理员 0 – 国家)here
- 安装 GGIS 编辑器
- 解压缩 AUT_adm_shp.zip
- 启动GGIS-Editor
- 选择选项卡:层 -> 添加层 -> 添加矢量层
选择选项卡:图层 -> 另存为
- 另存为 GeoJSON 文件
当您以这种格式将所有 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/