【问题标题】:Check if point exists within a polygon in react-native-maps?检查点是否存在于 react-native-maps 的多边形中?
【发布时间】:2018-07-13 15:43:58
【问题描述】:
我有一个 API,它返回定义地图(多边形)上区域边界的纬度和经度数组。
在我的 react-native 应用程序中,我安装了 react-native-maps。如何检查用户的位置是否在 api 返回的多边形中?
我知道这可以通过带有containsLocation() 功能的谷歌地图网络来实现。 react-native-maps 是否存在类似的功能?
【问题讨论】:
标签:
google-maps
react-native
polygon
react-native-maps
【解决方案2】:
对于仍在寻找更简单替代方案的人(或像我需要的那样寻求Circle 支持),请考虑使用geolib:https://github.com/manuelbieh/Geolib。
安装简单明了:
$yarn add geolib
然后在您的 React Native 项目中,使用以下命令导入它:
import geolib from 'geolib'
然后按照 README.md 中的说明使用 API。
【解决方案3】:
geolibe
是最好的解决方案。
react-native-geo-fencing 已被弃用,它会给您的项目带来更多错误
react-native-geo-fence 不是你想要的
【解决方案4】:
我的代码检查纬度,经度是否在许多多边形的对象内。如果坐标位于其中一个多边形内,则返回 true。
isInsidePoly = (lat, lon, multiPolycoords) => {
// ray-casting algorithm based on
// https://wrf.ecse.rpi.edu/Research/Short_Notes/pnpoly.html/pnpoly.html
var x = lat,
y = lon;
var inside = false;
multiPolycoords.map(poly => {
vs = poly;
for (var i = 0, j = vs.length - 1; i < vs.length; j = i++) {
var xi = vs[i].latitude,
yi = vs[i].longitude;
var xj = vs[j].latitude,
yj = vs[j].longitude;
var intersect =
yi > y != yj > y && x < ((xj - xi) * (y - yi)) / (yj - yi) + xi;
if (intersect) inside = !inside;
}
});
return inside;
};