Текущее местоположение (синий круг) заменено аннотацией - PullRequest
0 голосов
/ 27 апреля 2020

На изображении ниже аннотация «Мое местоположение» должна быть синим кружком. Вместо этого я получаю аннотацию к шару. Я почти уверен, что это как-то связано с последним блоком кода, но я не знаю, как это исправить. Окружающие аннотации в порядке - это места, которые я добавил на карту.

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

1 Ответ

1 голос
/ 27 апреля 2020

Вам необходимо вернуть 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
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...