简体   繁体   English

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

[英]MKPolygon - How to determine if a CLLocationCoordinate2D is in a CLLocationCoordinate2D polygon?

I have the swift code below that draws a polygon and drops an annotation on MKMapView.我有下面的 swift 代码,它绘制一个多边形并在 MKMapView 上放置一个注释。 I am trying to figure out how i could identify if the annotation's coordinate is within the polygon?我想弄清楚如何识别注释的坐标是否在多边形内?

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
    }
}

I created a Swift version of the answer by @StefanS and made it easier to read by porting the code from this answer我创建了@StefanS 答案的 Swift 版本,并通过移植此答案中的代码使其更易于阅读

    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
    }
- (BOOL)isPoint:(MKMapPoint)point insidePolygon:(MKPolygon *)poly {
  MKMapPoint *a =  poly.points;
  BOOL isInsidePolygon = NO;

  double testx = point.x;
  double testy = point.y;

  NSUInteger i = 0, j = 0, nvert = [poly pointCount];

  for (i = 0,  j = nvert - 1; i < nvert; j = i++) {
    if (((a[i].y >= testy) != (a[j].y >= testy)) &&
        (testx <= (a[j].x - a[i].x) * (testy - a[i].y) / (a[j].y - a[i].y) + a[i].x)) {
          isInsidePolygon = !isInsidePolygon;
        }
  }

  return isInsidePolygon;
}

Because the points xy are in Mercator projection coordinates, this makes sense and you don't need to convert to spherical coordinates for the calculation.因为点 xy 在墨卡托投影坐标中,这是有道理的,您不需要转换为球坐标进行计算。

Why this is a correct calculation here .为什么这是一个正确的计算在这里

EDIT: updated so that the calculation also considers a point on the line as inside the poly.编辑:更新以便计算也将线上的一个点视为多边形内部。

How about using MkPolygon.intersect()?使用 MkPolygon.intersect() 怎么样? This requires converting the point to a tiny MKMapRect, but it is simpler and may get some acceleration from the underlying API:这需要将点转换为一个很小的 ​​MKMapRect,但它更简单,并且可以从底层 API 获得一些加速:

func pointIsInside(point: MKMapPoint, polygon: MKPolygon) -> Bool {
    let mapRect = MKMapRectMake(point.x, point.y, 0.0001, 0.0001)
    return polygon.intersects(mapRect)
}

You can create a polygon renderer object and check if it's path contains the point based on the location in the map:您可以创建一个多边形渲染器对象,并根据地图中的位置检查它的路径是否包含该点:

func checkIf(_ location: CLLocationCoordinate2D, areInside polygon: MKPolygon) -> Bool {
    let polygonRenderer = MKPolygonRenderer(polygon: polygon)
    let mapPoint = MKMapPointForCoordinate(location)
    let polygonPoint = polygonRenderer.point(for: mapPoint)

    return polygonRenderer.path.contains(polygonPoint)
}

Method to be attached to a gesture recognizer:附加到手势识别器的方法:

@objc
func mapTapped(_ tap: UILongPressGestureRecognizer) {

    if tap.state == .recognized {
        let touchPoint = tap.location(in: mapView)
        let coord = mapView.convert(touchPoint, toCoordinateFrom: mapView)

        for overlay: MKOverlay in mapView.overlays {

            if let polygon = overlay as? MKPolygon {
                let renderer = MKPolygonRenderer(polygon: polygon)
                let mapPoint = MKMapPoint(coord)
                let rendererPoint = renderer.point(for: mapPoint)

                if renderer.path.contains(rendererPoint) {
                    // here comes your code
                }
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM