当前位置(蓝色圆圈)已被注释取代

Current location (blue circle) has been replaced by annotation

在下图中,'My Location' 注释应该是一个蓝色圆圈。相反,我得到了气球注释。我很确定它与最后一段代码有关,但我不知道如何修复它。周围的注释很好 - 这些是我添加到地图的地方。

我删除了不相关的代码:

class ExploreViewController: UIViewController, UISearchBarDelegate {

    @IBOutlet weak var exploreMapView: MKMapView!

    let locationManger = CLLocationManager()
    let regionInMeters: Double = 5000

    override func viewDidLoad() {
        super.viewDidLoad()

        checkLocationServices()
        getSchoolMarkers()
    }

    @IBAction func getCurrentLocation(_ sender: UIButton) {
        centerViewOnUserLocation()
    }

    func setupLocationManager() {
        locationManger.delegate = self
        locationManger.desiredAccuracy = kCLLocationAccuracyBest
    }

    func centerViewOnUserLocation() {
        if let userLocation = locationManger.location?.coordinate {
            let userRegion = MKCoordinateRegion.init(center: userLocation, latitudinalMeters: regionInMeters, longitudinalMeters: regionInMeters)
            exploreMapView.setRegion(userRegion, animated: true)
        }
    }

    func checkLocationServices() {
        if CLLocationManager.locationServicesEnabled() {
            setupLocationManager()
            exploreMapView.showsUserLocation = true
            centerViewOnUserLocation()
            locationManger.startUpdatingLocation()
        }
    }

    func getSchoolMarkers() {
        // Code for creating annotations removed
        self.exploreMapView.addAnnotation(schoolMarker)
    }
}

extension ExploreViewController: CLLocationManagerDelegate {

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        guard let userLocation = locations.last else {return}
        let currentLocation = CLLocationCoordinate2D(latitude: userLocation.coordinate.latitude, longitude: userLocation.coordinate.longitude)
        let userRegion = MKCoordinateRegion.init(center: currentLocation, latitudinalMeters: regionInMeters, longitudinalMeters: regionInMeters)
        exploreMapView.setRegion(userRegion, animated: true)
    }

    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        checkLocationAuthorization()
    }
}

extension ExploreViewController: MKMapViewDelegate {

    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        var view = exploreMapView.dequeueReusableAnnotationView(withIdentifier: "reuseIdentifier") as? MKMarkerAnnotationView
        if view == nil {
            view = MKMarkerAnnotationView(annotation: nil, reuseIdentifier: "reuseIdentifier")
        }
        view?.annotation = annotation
        view?.displayPriority = .required
        return view
    }
}

您需要 return nil 以获得 MKUserLocation 以获得默认注释视图:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {

    guard !annotation is MKUserLocation else {
        return nil
    }

    var view = exploreMapView.dequeueReusableAnnotationView(withIdentifier: "reuseIdentifier") as? MKMarkerAnnotationView
    if view == nil {
        view = MKMarkerAnnotationView(annotation: nil, reuseIdentifier: "reuseIdentifier")
    }
    view?.annotation = annotation
    view?.displayPriority = .required
    return view
}