如何在React Native Expo中检查自定义位置是否在多边形内部?

时间:2019-07-23 09:36:39

标签: react-native expo geofencing

我有一个使用Expo的React Native项目。我已经使用react本机地图显示带有多边形的地图以显示城市。 我有自动完成下拉列表。现在,我要检查所选地址是否在多边形内部或否。

我尝试了诸如react-native-geo-fencing和react-native-contains-location之类的节点模块,但由于它们都是react-native模块,因此两者都不适用于expo。

我尝试了这段代码,但始终会出现错误“ null不是对象”

const polygon = [
  { lat: 3.1336599385978805, lng: 101.31866455078125 },
  { lat: 3.3091633559540123, lng: 101.66198730468757 },
  { lat: 3.091150714460597, lng: 101.92977905273438 },
  { lat: 3.1336599385978805, lng: 101.31866455078125 } 
];

let point = {
  lat: 3.3091633559540123,
  lng: 101.66198730468757
};

containsLocation(point, polygon)
  .then(response => {
    // if the promise was success return TRUE
    console.log('Im inside of the polygon?', response)
  })
  .catch(error => {
    //only return FALSE if the point is not inside of the polygon
    console.log('Im outside of the polygon?', error)
  })

如果所选位置不在城市,我想显示错误消息。

0 个答案:

没有答案