当前位置(蓝色圆圈)已被注释替换

问题描述 投票:0回答:1

在下图中,“我的位置”注释应为蓝色圆圈。相反,我得到了气球注释。我很确定它与最后一段代码有关,但我不知道如何解决。周围的注释很好-我已在地图上添加了这些地方。

Screenshot

我已经删除了不相关的代码段:

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: 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
    }
}

ios swift mapkit
1个回答
0
投票

您需要为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
}
© www.soinside.com 2019 - 2024. All rights reserved.