首页 > 解决方案 > 在 MKMapView 中的注释周围画一个圆圈

问题描述

我正在尝试围绕注释画一个圆圈以显示将被地理围栏的半径。

在我的 ViewController 上方,我有一个处理用户搜索结果的协议

protocol HandleMapSearch {
    func dropPinZoomIn (placemark:MKPlacemark)
    func setUpGeofencing (placemark:MKPlacemark)
    func addRadiusCircle(placemark: MKPlacemark)
}

在 ViewController 类中,我有一个range由滑块设置的 var。在我的 viewDidLoadmapView.delegate = self 和 ViewController 类中,我有我的 mapView 委托函数,它应该有助于绘制我的圆圈

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
    if overlay is MKCircle {
        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
    } else {
        return MKPolylineRenderer()
    }
}

我的 addRadiusCircle 方法看起来像这样

func addRadiusCircle(placemark: MKPlacemark){
    let circle = MKCircle(center: (placemark.location!.coordinate), radius: CLLocationDistance(range))
    mapView.add(circle)
}

当我测试我的应用程序并搜索位置时,我在该位置的坐标处得到一个注释,但它周围没有圆圈。我尝试绘制圆圈的方式有问题吗?任何帮助将不胜感激。

标签: iosswiftmapkitmkoverlay

解决方案


推荐阅读