在地图上绘制圆圈不会显示

时间:2017-07-21 16:20:09

标签: swift mapkit

我无法弄清楚如何在询问半径的提示中使用用户输入来渲染我创建的圆圈。我从另一个类继承了userMapView,但是在该类中没有对叠加渲染做任何事情,所以我从来没有使用过rendererForOverlay函数。我是否需要在父类中添加该函数或者我是否正在做其他错误的操作?

@IBAction func addGeoFencePushed(_ sender: Any) {
    let alertController = UIAlertController(title: nil, message: "How large would you like the geofence to be?", preferredStyle: .alert)
    let confirmAction = UIAlertAction(title: "Confirm", style: .default) { (_) in
        if let field = alertController.textFields?[0] {
            // store your data
            ALGlobal.sharedInstance.globalDefaults.set(field.text! as String, forKey: "geoFenceSize")
            let rad = field.text!
            self.drawGeoFence(radius: rad)
        } else {
            // user did not fill field
        }
    }
    self.present(alertController, animated: true, completion: nil)
}

func drawGeoFence(radius: String){
    let bagList =  ALGlobal.sharedInstance.bagLists
    let bag = bagList?[0]
    let center = CLLocationCoordinate2D(
        latitude: CLLocationDegrees.init(bag!.latitude),
        longitude: CLLocationDegrees.init(bag!.longitude))
    let circle = MKCircle(center: center, radius: Double(radius)!)

    userMapView.add(circle)
}

func mapView(mapView: MKMapView,rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer {

    let circleRenderer = MKCircleRenderer(overlay: overlay)
    circleRenderer.fillColor = UIColor.blue.withAlphaComponent(0.1)
    circleRenderer.strokeColor = UIColor.blue
    circleRenderer.lineWidth = 1
    return circleRenderer
}

1 个答案:

答案 0 :(得分:1)

您的渲染器功能未被调用,因为它的签名错误。在Swift 3中,正确的签名是

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {

此外,您需要确保self是地图视图的委托。