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

时间:2015-08-21 20:50:49

标签: ios swift mapkit mkpolyline mkpolygon

此时此刻,我正在试图弄清楚MKMapView上的坐标是否在提前拉出的纬度/经度的MKPolygon内。

我正在使用CGPathContainsPoint来确定坐标是否在地图上的多边形内,但无论我选择哪个坐标,它总是返回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
    }

3 个答案:

答案 0 :(得分:8)

更新SWIFT 4

您正在混淆CGPoints,它们是x和y坐标对,指向视图上的位置使用CLLocationCoordinate2Ds,它们是地球上的坐标。

对于CGPathContainsPoint,您希望传递polygonRenderer路径(从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就会调用它,这与此完全无关。

答案 1 :(得分:5)

针对Swift 3进行了更新

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

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

答案 2 :(得分:0)

Swift 5解决方案:

由于renderer.path的contains方法需要将点指定为CGPoint,因此需要通过MKMapPoint将其从地理坐标转换为重做器的点,否则结果将是意外的。

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

相关问题