I have a react native project using Expo. I have used react native maps to show map with a polygon to show a city. I have autocomplete dropdown. Now I want to check if selected address is inside the polygon or no.
I have tried node modules like react-native-geo-fencing & react-native-contains-location but both doesn’t work with expo as they are react-native modules.
I tried this code but it always gives error “null is not an object”
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(‘I am outside of the polygon?’, error)
})
I want to show error message if selected location is outside the city.
I would suggest searching for things like “javascript polygon contains point” or “javascript check if point is within a polygon” etc. and looking for pure JavaScript libraries that do what you want.
When I did that I came across this stack overflow question:
Note: I got an error which I do not understand when I used the turf.polygon() function with the above polygon, but as far as I know turf.polygon() is just a convenience function for generating GeoJSON, so including the GeoJSON directly works around the problem.