Raf A
Raf A

Reputation: 179

Current location (blue circle) has been replaced by annotation

In the image below, the 'My Location' annotation should be a blue circle. Instead, I get the balloon annotation. I'm pretty sure it has something to do with the last block of code but I don't know how to fix it. The surrounding annotations are fine - these a places I've added to the map.

Screenshot

I've removed the irrelevant bits of code:

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

Upvotes: 0

Views: 487

Answers (1)

Paulw11
Paulw11

Reputation: 114875

You need to return nil for the MKUserLocation in order to get the default annotation view:

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
}

Upvotes: 3

Related Questions