ios - MKPolygon - 如何确定 CLLocationCoordinate2D 是否在 CLLocationCoordinate2D 多边形中?

标签 ios swift mapkit mkpolygon

我有下面的 swift 代码,它绘制一个多边形并在 MKMapView 上放置一个注释。我想弄清楚如何确定注释的坐标是否在多边形内?

import UIKit
import MapKit

class ViewController: UIViewController {

    @IBOutlet weak var mapView: MKMapView!

    override func viewDidLoad() {
        super.viewDidLoad()
        let initialLocation = CLLocation(latitude: 49.140838, longitude: -123.127886)
        centerMapOnLocation(initialLocation)
        addBoundry()

        var annotation = MKPointAnnotation()
        annotation.coordinate = point1
        annotation.title = "Roatan"
        annotation.subtitle = "Honduras"

        mapView.addAnnotation(annotation)
    }

    var points = [CLLocationCoordinate2DMake(49.142677,  -123.135139),
                  CLLocationCoordinate2DMake(49.142730, -123.125794),
                  CLLocationCoordinate2DMake(49.140874, -123.125805),
                  CLLocationCoordinate2DMake(49.140885, -123.135214)]

    var point1 = CLLocationCoordinate2DMake(49.141821, -123.131577)

    func addBoundry() {
        let polygon = MKPolygon(coordinates: &points, count: points.count)

        mapView.addOverlay(polygon)
    }

    let regionRadius: CLLocationDistance = 1000

    func centerMapOnLocation(location: CLLocation) {
        let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate, regionRadius * 2.0, regionRadius * 2.0)

        mapView.setRegion(coordinateRegion, animated: true)
    }

    func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
        if overlay is MKPolygon {
            let polygonView = MKPolygonRenderer(overlay: overlay)
            polygonView.strokeColor = UIColor.magentaColor()

            return polygonView
        }

        return nil
    }
}

最佳答案

我创建了@StefanS 的答案的 Swift 版本,并通过移植此 answer 中的代码使其更易于阅读

    func isPoint(point: MKMapPoint, insidePolygon poly: MKPolygon) -> Bool {

        let polygonVerticies = poly.points()
        var isInsidePolygon = false

        for i in 0..<poly.pointCount {
            let vertex = polygonVerticies[i]
            let nextVertex = polygonVerticies[(i + 1) % poly.pointCount]

            // The vertices of the edge we are checking.
            let xp0 = vertex.x
            let yp0 = vertex.y
            let xp1 = nextVertex.x
            let yp1 = nextVertex.y

            if ((yp0 <= point.y) && (yp1 > point.y) || (yp1 <= point.y) && (yp0 > point.y))
            {
                // If so, get the point where it crosses that line. This is a simple solution
                // to a linear equation. Note that we can't get a division by zero here -
                // if yp1 == yp0 then the above if be false.
                let cross = (xp1 - xp0) * (point.y - yp0) / (yp1 - yp0) + xp0

                // Finally check if it crosses to the left of our test point. You could equally
                // do right and it should give the same result.
                if cross < point.x {
                    isInsidePolygon = !isInsidePolygon
                }
            }
        }

        return isInsidePolygon
    }

关于ios - MKPolygon - 如何确定 CLLocationCoordinate2D 是否在 CLLocationCoordinate2D 多边形中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31469835/

相关文章:

swift - 当 UITextfield 上发生 touchup inside 事件时导航到另一个 View

swift - 在 Swift 中跳转到 map 应用程序

objective-c - iOS 和 Android 上两点之间的不同距离

xcode - 在 Swift Xcode 7.3 中查找当前位置

android - 是否可以通过 IOS 或 Android 商店进行深度链接(将数据从 url 传递到应用程序安装)

ios - UITableView 编辑中的多项选择 - 最多选择两行?

android - NoSuchMethodError : The method 'map' was called on null. flutter

swift - 等于或==在swiftui中抛出 "Ambiguous reference"错误

ios - Swift:删除层,而不删除 UITableViewCell 中的 UIView

ios - Swift 4 iOS - 在不影响 Storyboard字体大小的情况下完全覆盖系统字体