斯威夫特:多边形中的点?如何检查用户的位置是不是在 Geo-JSON 多边形内?
Posted
技术标签:
【中文标题】斯威夫特:多边形中的点?如何检查用户的位置是不是在 Geo-JSON 多边形内?【英文标题】:Swift: Point in polygon? How can I check if a user's location falls within a Geo-JSON polygon?斯威夫特:多边形中的点?如何检查用户的位置是否在 Geo-JSON 多边形内? 【发布时间】:2015-05-16 07:17:21 【问题描述】:我有一个用户的位置坐标。鉴于该位置位于其中一个多边形中,我如何使用 Swift 或 Objective-C 通过 OBJECTID 返回它落在哪个多边形中?
我使用的示例 JSON 多边形数据如下所示:
"type": "Feature", "properties": "OBJECTID": 10, "District_N": "10", "WARD": 10 , "geometry": "type": "Polygon", "coordinates": [ [ [ -87.52389, 41.742767 ], [ -87.524668, 41.644637 ], [ -87.578588, 41.64473 ], [ -87.580619, 41.65028 ], [ -87.601709, 41.676572 ], [ -87.60172, 41.685837 ], [ -87.592251, 41.703002 ], [ -87.592202, 41.707656 ], [ -87.586051, 41.707765 ], [ -87.585322, 41.709059 ], [ -87.585162, 41.720732 ], [ -87.563165, 41.698022 ], [ -87.563275, 41.706395 ], [ -87.559645, 41.706399 ], [ -87.559775, 41.715404 ], [ -87.569434, 41.715639 ], [ -87.566814, 41.716484 ], [ -87.565902, 41.722585 ], [ -87.556604, 41.722701 ], [ -87.556693, 41.726351 ], [ -87.554907, 41.726374 ], [ -87.557838, 41.729746 ], [ -87.552591, 41.728229 ], [ -87.552639, 41.730057 ], [ -87.555405, 41.730014 ], [ -87.555585, 41.737329 ], [ -87.556887, 41.737313 ], [ -87.555191, 41.744277 ], [ -87.551526, 41.742147 ], [ -87.550325, 41.744674 ], [ -87.541923, 41.744779 ], [ -87.541772, 41.741144 ], [ -87.529645, 41.741412 ], [ -87.530874, 41.748023 ], [ -87.544007, 41.753548 ], [ -87.540739, 41.757435 ], [ -87.524445, 41.760035 ], [ -87.52389, 41.742767 ] ] ]
,
"type": "Feature", "properties": "OBJECTID": 11, "District_N": "11", "WARD": 11 , "geometry": "type": "Polygon", "coordinates": [ [ [ -87.647655, 41.874319 ], [ -87.645662, 41.874331 ], [ -87.644767, 41.867185 ], [ -87.639218, 41.867254 ], [ -87.639014, 41.860018 ], [ -87.64194, 41.859984 ], [ -87.641864, 41.857621 ], [ -87.639967, 41.857648 ], [ -87.644938, 41.852666 ], [ -87.638532, 41.852792 ], [ -87.640478, 41.849467 ], [ -87.631965, 41.848252 ], [ -87.629944, 41.846683 ], [ -87.629594, 41.83097 ], [ -87.631504, 41.830939 ], [ -87.631465, 41.829064 ], [ -87.633842, 41.827265 ], [ -87.633763, 41.823616 ], [ -87.636354, 41.823583 ], [ -87.635839, 41.809046 ], [ -87.640679, 41.808952 ], [ -87.641275, 41.805025 ], [ -87.645432, 41.80494 ], [ -87.645538, 41.808864 ], [ -87.65524, 41.808734 ], [ -87.655246, 41.81237 ], [ -87.665064, 41.812222 ], [ -87.665568, 41.830501 ], [ -87.657712, 41.830726 ], [ -87.66436, 41.839375 ], [ -87.664449, 41.844617 ], [ -87.658043, 41.847724 ], [ -87.646473, 41.84919 ], [ -87.646495, 41.852647 ], [ -87.648132, 41.852613 ], [ -87.647673, 41.859898 ], [ -87.656479, 41.859765 ], [ -87.656554, 41.862513 ], [ -87.650937, 41.863499 ], [ -87.652815, 41.873034 ], [ -87.647655, 41.874319 ] ] ]
【问题讨论】:
你能提供一个测试点吗? 【参考方案1】:创建一个 Bezier 路径(确保关闭它)然后使用 containsPoint 方法。您可以使用 usesEvenOddFillRule 属性选择“包含”算法。更多信息可在 Apple 的文档中找到。
【讨论】:
以上是关于斯威夫特:多边形中的点?如何检查用户的位置是不是在 Geo-JSON 多边形内?的主要内容,如果未能解决你的问题,请参考以下文章