ios - 如何在 MapKit 上绘制围绕多个注释/坐标的圆形叠加层?

标签 ios swift geometry mapkit mapkitannotation

我有一个数组[CLLocationCooperative2D],我想在MKMapView上围绕所有这些坐标画一个圆圈。

我已经成功地围绕单个 CLLocationCooperative2D 画了一个圆,如下所示:

let coordinate = CLLocationCoordinate2D(latitude: 53, longitude: 27)
self.mapView.add(MKCircle(center: coordinate, radius: 100))

extension MapViewController: MKMapViewDelegate {
    func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
        guard overlay is MKCircle else { return MKOverlayRenderer() }

        let circle = MKCircleRenderer(overlay: overlay)
        circle.strokeColor = UIColor.red
        circle.fillColor = UIColor(red: 255, green: 0, blue: 0, alpha: 0.1)
        circle.lineWidth = 1
        return circle
    }
}

如何绘制一个包围/包含所有坐标的圆?,如下所示:

enter image description here

最佳答案

我想出了MKCooperativeRegion初始化器,它提供了坐标区域,扩展有一个计算属性来提供区域的半径。

extension MKCoordinateRegion {

    init?(from coordinates: [CLLocationCoordinate2D]) {
        guard coordinates.count > 1 else { return nil }

        let a = MKCoordinateRegion.region(coordinates, fix: { $0 }, fix2: { $0 })
        let b = MKCoordinateRegion.region(coordinates, fix: MKCoordinateRegion.fixMeridianNegativeLongitude, fix2: MKCoordinateRegion.fixMeridian180thLongitude)

        guard (a != nil || b != nil) else { return nil }
        guard (a != nil && b != nil) else {
            self = a ?? b!
            return
        }

        self = [a!, b!].min(by: { $0.span.longitudeDelta < $1.span.longitudeDelta }) ?? a!
    }

    var radius: CLLocationDistance {
        let furthest = CLLocation(latitude: self.center.latitude + (span.latitudeDelta / 2),
                                  longitude: center.longitude + (span.longitudeDelta / 2))
        return CLLocation(latitude: center.latitude, longitude: center.longitude).distance(from: furthest)
    }

    // MARK: - Private
    private static func region(_ coordinates: [CLLocationCoordinate2D],
                               fix: (CLLocationCoordinate2D) -> CLLocationCoordinate2D,
                               fix2: (CLLocationCoordinate2D) -> CLLocationCoordinate2D) -> MKCoordinateRegion? {
        let t = coordinates.map(fix)
        let min = CLLocationCoordinate2D(latitude: t.min { $0.latitude < $1.latitude }!.latitude,
                                         longitude: t.min { $0.longitude < $1.longitude }!.longitude)
        let max = CLLocationCoordinate2D(latitude: t.max { $0.latitude < $1.latitude }!.latitude,
                                         longitude: t.max { $0.longitude < $1.longitude }!.longitude)

        // find span
        let span = MKCoordinateSpanMake(max.latitude - min.latitude, max.longitude - min.longitude)

        // find center
        let center = CLLocationCoordinate2D(latitude: max.latitude - span.latitudeDelta / 2,
                                            longitude: max.longitude - span.longitudeDelta / 2)

        return MKCoordinateRegion(center: fix2(center), span: span)
    }

    private static func fixMeridianNegativeLongitude(coordinate: CLLocationCoordinate2D) -> CLLocationCoordinate2D {
        guard (coordinate.longitude < 0) else { return coordinate }

        let fixedLng = 360 + coordinate.longitude
        return CLLocationCoordinate2D(latitude: coordinate.latitude, longitude: fixedLng)
    }

    private static func fixMeridian180thLongitude(coordinate: CLLocationCoordinate2D) -> CLLocationCoordinate2D {
        guard (coordinate.longitude > 180) else { return coordinate }

        let fixedLng = -360 + coordinate.longitude
        return CLLocationCoordinate2D(latitude: coordinate.latitude, longitude: fixedLng)
    }

}

用法:

let coordinates: [CLLocationCoordinate2D] = self.mapView.annotations.map{ $0.coordinate }
if let region = MKCoordinateRegion(from: coordinates) {
    self.mapView.add(MKCircle(center: region.center, radius: region.radius))
}

结果正是我想要的,能够处理穿过 180 度子午线的坐标:

enter image description here

关于ios - 如何在 MapKit 上绘制围绕多个注释/坐标的圆形叠加层?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49034729/

相关文章:

iOS:问题修改按钮的颜色

swift - 比较 SKSpriteNode 纹理属性

Swift:为什么变异函数不能是静态的

python - 给定二维空间中的一组边界框,将它们分组到行中

ios - 如何更改幻灯片菜单的 View Controller ?

ios - Beacon/IOS CLLocationManager 获取当前区域

swift - UITabBarController自定义页面顶栏颜色swift

geometry - 多边形之间的交点作为线剪

java - 如何测试两个任意形状的重叠(或碰撞)

ios - 如何在 iOS 中获取推特个人资料图片?