ios - 确定纬度/经度点是否在 Mapview 中的 MKPolygon 中?

标签 ios swift mapkit mkpolyline mkpolygon

此刻,我试图弄清楚 MKMapView 上的坐标是否在提前绘制的带有纬度/经度的 MKPolygon 内。

我正在使用 CGPathContainsPoint 来确定坐标是否在 map 上的多边形内,但无论我选择的坐标如何,它总是返回 false。

谁能解释一下到底出了什么问题?下面是我在 Swift 中的代码。

class ViewController: UIViewController, MKMapViewDelegate

@IBOutlet weak var mapView: MKMapView!

    let initialLocation = CLLocation(latitude: 43.656734, longitude: -79.381576)
    let point = CGPointMake(43.656734, -79.381576)
    let regionRadius: CLLocationDistance = 500
    let point1 = CLLocationCoordinate2D(latitude: 43.656734, longitude: -79.381576)

    var points = [CLLocationCoordinate2DMake(43.655782, -79.382094),
        CLLocationCoordinate2DMake(43.657499, -79.382310),
        CLLocationCoordinate2DMake(43.656656, -79.380497),
        CLLocationCoordinate2DMake(43.655782, -79.382094)]

    override func viewDidLoad() {
        super.viewDidLoad()

        centerMapOnLocation(initialLocation)

        let polygon = MKPolygon(coordinates: &points, count: points.count)
        mapView.addOverlay(polygon)

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

        mapView.addAnnotation(annotation)
        self.mapView.delegate = self

    }

    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.redColor()

            if  CGPathContainsPoint(polygonView.path, nil, CGPointMake(43.656734, -79.381576), true) {
                print("True!!!!!")
            } else {
                println("False")
            }

            return polygonView
        }
        return nil
    }

最佳答案

针对 SWIFT 4 进行了更新

您混淆了 CGPoints,它是指向您 View 中某个位置的 x 和 y 坐标对, 与 CLLocationCoordinate2Ds,它们是地球上的坐标。

对于 CGPathContainsPoint,您希望传入您的多边形渲染器路径(在 viewDidLoad() 中从您的多边形创建)和当前位置,如下所示:

let polygonRenderer = MKPolygonRenderer(polygon: polygon)
let mapPoint: MKMapPoint = MKMapPointForCoordinate(coordinate)
let polygonViewPoint: CGPoint = polygonRenderer.pointForMapPoint(mapPoint)

if polygonRenderer.path.contains(polygonViewPoint) {
    print("Your location was inside your polygon.")
}

因此,您想考虑应该将这段代码放在哪里,因为您当前拥有它的地方是每当在屏幕上绘制 MKOverlay 时都会调用的函数,而这与此完全无关。

关于ios - 确定纬度/经度点是否在 Mapview 中的 MKPolygon 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32149062/

相关文章:

ios - 错误 : "connection to service named com.apple.MapKit.SnapshotService" when starting MKMapSnapshotter

iOS UITableView 无法为每个单元格计算正确的单元格高度

ios - 类对于键 staticTexts 不符合键值编码

ios - Swift - 从图像创建 GIF 并将其转换为 NSData

ios - 为什么我不能使用 `isCalculating` 属性来等待计算请求完成

iOS 如何在点击后将 MKMapView 置于 MKAnnotationView 标注附件的中心?

ios - 如何在 Swift 中访问结构体类型数组中的变量?

iOS-如何在 UITableview 中使用数组快速在一个原型(prototype) Cell 中显示 40 张图像?

ios - Swift 预期 ',' 分隔符错误

ios - 未调用 UITextFieldDelegate 方法