检查坐标是否位于多边形中
Check if a coordinate lie in a Polygon
我在数据库中有几个构成以下多边形的坐标,我想检查用户 requested/coordinates 是否落在这个红色区域下,那么它应该 return 为真,如果requested/coordinates 在这个区域之外,它应该 return false。
我在后端使用 Laravel 和 Js,并为此使用 google 地图。
我只想制作一个 API 来检查它是否位于当前区域。
你应该检查 turf.js https://turfjs.org/ 这是一个空间分析库。
方法turf.booleanPointInPolygon()
正是您想要的。
https://turfjs.org/docs/#booleanPointInPolygon
来自站点文档:
Takes a Point and a Polygon or MultiPolygon and determines if the
point resides inside the polygon. The polygon can be convex or
concave. The function accounts for holes.
您只需要拥有 GeoJSON 格式的点和多边形(turf.js 也有从其他格式转换为 GeoJSON 的方法)。
我喜欢这个问题!答案很酷!
您需要算出 X 轴穿过多边形的次数。如果数字是奇数,则在您的范围内,如果是偶数或零,则在您的范围外!
这里有一个 link 描述。 https://www.geeksforgeeks.org/how-to-check-if-a-given-point-lies-inside-a-polygon/
我在数据库中有几个构成以下多边形的坐标,我想检查用户 requested/coordinates 是否落在这个红色区域下,那么它应该 return 为真,如果requested/coordinates 在这个区域之外,它应该 return false。
我在后端使用 Laravel 和 Js,并为此使用 google 地图。 我只想制作一个 API 来检查它是否位于当前区域。
你应该检查 turf.js https://turfjs.org/ 这是一个空间分析库。
方法turf.booleanPointInPolygon()
正是您想要的。
https://turfjs.org/docs/#booleanPointInPolygon
来自站点文档:
Takes a Point and a Polygon or MultiPolygon and determines if the point resides inside the polygon. The polygon can be convex or concave. The function accounts for holes.
您只需要拥有 GeoJSON 格式的点和多边形(turf.js 也有从其他格式转换为 GeoJSON 的方法)。
我喜欢这个问题!答案很酷!
您需要算出 X 轴穿过多边形的次数。如果数字是奇数,则在您的范围内,如果是偶数或零,则在您的范围外!
这里有一个 link 描述。 https://www.geeksforgeeks.org/how-to-check-if-a-given-point-lies-inside-a-polygon/