如果您还在寻找更简便的替代方案(或者像我一样需要支持 Circle
的支持),请考虑使用 geolib
:https://github.com/manuelbieh/Geolib。
安装过程非常简单:
$yarn add geolib
然后,在您的 React Native 项目中导入它:
import geolib from 'geolib'
接下来,只需按照 README.md 中的说明使用 API 即可。
react-native-maps
并没有暴露该功能,但是我知道有两个包可以帮助你:
react-native-geo-fencing可以执行与你提到的Google Maps实用程序相同的操作。
react-native-geo-fence可以为你处理位置检查,并公开你可以连接到的事件。
isPointInPolygon(point, polygon)
函数。 - delkantgeolibe
是最佳解决方案。
react-native-geo-fencing
已经过时,会给您的项目带来更多错误,
而react-native-geo-fence
不是您想要的。
我的代码用于检查经纬度是否在多边形对象内部。如果坐标在其中一个多边形内部,则返回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;
ERROR TypeError: Cannot read property isPointInPolygon of undefined, js engine: hermes
。 - Tinkaal Gogoi