【问题标题】:Determining if Latitude/Longitude Point is in a MKPolygon within Mapview?确定纬度/经度点是否在 Mapview 中的 MKPolygon 中?
【发布时间】:2015-08-21 20:50:49
【问题描述】:

此时,我正在尝试确定 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
    }

【问题讨论】:

  • 嘿,你不会使用“false”(获取蜿蜒风格的路径).. CGPathContainsPoint 的最后一个参数。
  • 真假都试过了,没用。 :(

标签: ios swift mapkit mkpolyline mkpolygon


【解决方案1】:

为 SWIFT 4 更新

您将 CGPoints 与 CLLocationCoordinate2Ds(地球上的坐标)混淆了,CGPoints 是 x 和 y 坐标对,指向您视图上的某个位置

对于 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 被绘制到屏幕上时就会被调用,这与这个完全无关。

【讨论】:

    【解决方案2】:

    为 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.")
    }
    

    【讨论】:

    • 您在 MKPolygonRenderer(polygon: polygon) 上传递的内容,因为我们只有在用户按下折线后获得地图点
    【解决方案3】:

    Swift 5 的解决方案:

    由于 renderer.path 的 contains 方法需要点为 CGPoint,我们需要通过 MKMapPoint 将其从地理坐标转换为 rederer 的点,否则结果将出乎意料。

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

    【讨论】:

      猜你喜欢
      • 2011-03-10
      • 2021-10-10
      • 1970-01-01
      • 1970-01-01
      • 2011-08-28
      • 1970-01-01
      • 2011-06-09
      • 2017-04-05
      • 1970-01-01
      相关资源
      最近更新 更多