代码之家  ›  专栏  ›  技术社区  ›  Lukas Bimba

swift mkcircle覆盖

  •  0
  • Lukas Bimba  · 技术社区  · 6 年前

    我试图在地图注释周围画一个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
    }
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Kosuke Ogawa    6 年前

    是否设置了MapView代理?

    self.mapView.delegate = self
    

    别忘了 MKMapViewDelegate 协议。

    class ViewController: UIViewController, MKMapViewDelegate  {
        ...
    }