Search code examples
iosswiftannotationsmkmapview

Mkmapview annotations appear only after panning twice


I'm having difficulties getting annotations to appear when panning or zooming to a new region. If I pan to a new map region, nothing appears, but if I do a small pan there, the annotations appear. I hope to have the annotations appearing without the user needing to pan multiple times.

The similar problems I can find seem to have been solved by loading the annotations on the main thread, but so far, probably inadvisably, I am running everything on the main thread.

I've included my regionDidChange and regionWillChange functions where I annotate my mapView and the annotate function.

  func mapView(_ mapView: MKMapView, regionWillChangeAnimated animated: Bool) {
    zoomLevelBeforeChange = ((mapView.getZoomLevel() * 100).rounded() / 100)
}

func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {

    var apiScale: String
    print("Zoom: \(mapView.getZoomLevel())")

    if mapView.getZoomLevel() < 5 {
        mapView.setCenter(coordinate: mapView.centerCoordinate, zoomLevel: 5, animated: true)
        apiScale = "6"
    } else if mapView.getZoomLevel() >= 5.0 && mapView.getZoomLevel() < 6.0 {
        apiScale = "6"
    } else if mapView.getZoomLevel() >= 6.0 && mapView.getZoomLevel() < 7.5 {
        apiScale = "7"
    } else if mapView.getZoomLevel() >= 7.5 && mapView.getZoomLevel() < 8.5 {
        apiScale = "9"
    } else if mapView.getZoomLevel() >= 8.5 && mapView.getZoomLevel() <= 9.5 {
        apiScale = "11"
    } else if mapView.getZoomLevel() >= 9.5 && mapView.getZoomLevel() <= 10.0 {
        apiScale = "13"
    } else if mapView.getZoomLevel() > 10 {
        mapView.setCenter(coordinate: mapView.centerCoordinate, zoomLevel: 10, animated: true)
        apiScale = "13"
    } else {
        apiScale = "0"
    }
    print(apiScale)

    if ((mapView.getZoomLevel() * 100).rounded() / 100) == zoomLevelBeforeChange {
        print("don't remove annotations")
    } else {
        let allAnnotations = self.mapView.annotations
        self.mapView.removeAnnotations(allAnnotations)
    }

    let latitudeDelta = mapView.region.span.latitudeDelta
    let longitudeDelta = mapView.region.span.longitudeDelta
    let centerCoordLat = mapView.centerCoordinate.latitude
    let centerCoordLong = mapView.centerCoordinate.longitude

    lowerLeftLong = (centerCoordLong - (longitudeDelta / 2))
    lowerLeftLat = (centerCoordLat - (latitudeDelta / 2))
    upperRightLong = (centerCoordLong + (longitudeDelta / 2))
    upperRightLat = (centerCoordLat + (latitudeDelta / 2))

    mapUrl = "http://api.openweathermap.org/data/2.5/box/city?bbox=\(lowerLeftLong!),\(lowerLeftLat!),\(upperRightLong!),\(upperRightLat!),\(apiScale)&appid=(appId)"

    downloadMapWeatherApi {
        annotate()
        self.mapAnnotations = []
    }
}


func downloadMapWeatherApi(completed: DownloadComplete) {
    Alamofire.request(self.mapUrl).responseJSON { response in
        let result = response.result
        if let dict = result.value as? Dictionary<String, AnyObject> {
            if let list = dict["list"] as? [Dictionary<String, AnyObject>] {

                for obj in list {
                    let annotation = MapAnnotation(locationDict: obj)
                    self.mapAnnotations.append(annotation)
                }
            }
        }
    }
    completed()
}


func annotate() {

    for location in self.mapAnnotations {
        let annotation = CustomAnnotation()
        annotation.title = location.cityName
        annotation.subtitle = "\(Int(location.temperature))°"
        annotation.attribute = location.weatherType
        annotation.coordinate = CLLocationCoordinate2D(latitude: location.latitude, longitude: location.longitude)
        mapView.addAnnotation(annotation)

    }
}

Thanks!


Solution

  • A very easy fix but took me a long time to figure out. I do not know the reason behind it but it appears my array of annotations was consistently from the last mapView rect. I moved my annotation function to inside my weather data api call and that rectified it.

        func downloadMapWeatherApi(completed: DownloadComplete) {
        Alamofire.request(self.mapUrl).responseJSON { response in
            let result = response.result
            if let dict = result.value as? Dictionary<String, AnyObject> {
                if let list = dict["list"] as? [Dictionary<String, AnyObject>] {
    
                    for obj in list {
                        let annotation = MapAnnotation(locationDict: obj)
                        self.mapAnnotations.append(annotation)
                    }
                    self.annotate()
                }
            }
        }
        completed()
    }
    

    Hopefully this can help someone with a similar issue.