小编M_J*_*ean的帖子

确定纬度/经度点是否在Mapview中的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 …
Run Code Online (Sandbox Code Playgroud)

mapkit ios mkpolygon mkpolyline swift

9
推荐指数
2
解决办法
3912
查看次数

标签 统计

ios ×1

mapkit ×1

mkpolygon ×1

mkpolyline ×1

swift ×1