确定纬度/经度点是否在Mapview中的MKPolygon中? [英] Determining if Latitude/Longitude Point is in a MKPolygon within Mapview?

查看:137
本文介绍了确定纬度/经度点是否在Mapview中的MKPolygon中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此刻,我正在尝试确定MKMapView上的坐标是否在提前绘制的经度/纬度的MKPolygon中.

At this moment, I am trying to figure out whether a coordinate on a MKMapView is within a MKPolygon drawn out ahead of time with latitudes/longitudes.

我正在使用CGPathContainsPoint来确定坐标是否在地图上的多边形内,但是无论我选择什么坐标,它总是返回false.

I am using CGPathContainsPoint to determine whether a coordinate is within the polygon on the map but it always returns false regardless of the coordinate I choose.

任何人都可以解释到底出了什么问题吗?下面是我在Swift中的代码.

Can anyone please explain what exactly is going wrong? Below is my code in 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更新

您将CGPoint(它们是指向视图上的位置 的x和y坐标对)与CLLocationCoordinate2Ds(地球上的坐标)相混淆.

You are confusing CGPoints, which are x and y coordinate pairs that point to a location on your view, with CLLocationCoordinate2Ds, which are coordinates on Earth.

对于CGPathContainsPoint,您想传递polygonRenderer路径(从viewDidLoad()中的多边形创建)和当前位置,如下所示:

For CGPathContainsPoint, you want to pass in your polygonRenderer path (created from your polygon in viewDidLoad()) and current location like this:

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时都会被调用,而这与此功能完全无关.

So you want to think about where you should be putting this code, as where you currently have it is inside the function that gets called whenever an MKOverlay gets drawn on screen, and that is completely unrelated to this.

这篇关于确定纬度/经度点是否在Mapview中的MKPolygon中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆