0

我正在尝试围绕地图注释绘制 MKCircle。我认为代码到目前为止是正确的,但不确定为什么它不起作用。我相信我拥有它工作所需的所有代码。

func getPlaces(){
    let uid = Auth.auth().currentUser?.uid
    Database.database().reference().child("Businesses").child(uid!).observeSingleEvent(of: .value, with: { (snapshot) in
        // print("\(snap.value)")

        if let locationDict = snapshot.value as? [String:AnyObject]{

            let lat = Double(locationDict["businessLatitude"] as! String)
            let long = Double(locationDict["businessLongitude"] as! String)
            let center = CLLocationCoordinate2D(latitude: lat!, longitude: long!)
            let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))

            let radius = 100.0

            self.mapView!.setRegion(region, animated: true)

            let circle = MKCircle(center: center, radius: radius)

            let annotation = MKPointAnnotation()
            annotation.coordinate = region.center
            self.mapView.addAnnotation(annotation)
            self.mapView.add(circle)
        }
    })
}

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
    let circleRenderer = MKCircleRenderer(overlay: overlay)
    circleRenderer.strokeColor = UIColor.red
    circleRenderer.lineWidth = 1.0
    return circleRenderer
}
4

1 回答 1

1

您是否设置了地图视图委托?

self.mapView.delegate = self

不要忘记MKMapViewDelegate协议。

class ViewController: UIViewController, MKMapViewDelegate  {
    ...
}
于 2018-05-20T15:24:04.737 回答