Fortaite
Fortaite

Reputation: 1

Delaying Add Annotation Until Search Has Finished

so i've searched and found multiple articles regarding creation of unique annotations and pins but I think i'm missing something more fundamental, possibly using the annotationView function under the MKMapViewDelegate.

First off i'm very new to Swift programming so apologies in advance if this is something obvious. Essentially I have a mapView which finds a users location with a zoom in animation. Then I have a button which passes the button title to an MKLocalSearch and a for/in loop which creates my annotations and adds them to my map view. The problem is the annotations are not added on the first click of my button, I have to click again for the annotations to appear.

I've experimented with DispatchQueue.main.async and trying to set a delay on this loop from executing to allow the response to complete for processing, here's the relevant section at the top of my viewController (I've only included the relevant code, I have an extension for my CLLocationManagerDelegate which works fine as mentioned before):

import UIKit
import CoreLocation
import MapKit

class MapViewController: UIViewController, MKMapViewDelegate {
    
    var weatherManager = WeatherManager()
    let locationManager = CLLocationManager()
    let request = MKLocalSearch.Request()
    var localSearch: MKLocalSearch?
    
    @IBOutlet weak var tempLabel: UILabel!
    @IBOutlet weak var mapView: MKMapView!
    @IBOutlet weak var conditionImageView: UIImageView!
    override func viewDidLoad() {
        super.viewDidLoad()
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestWhenInUseAuthorization()
        locationManager.requestLocation()
        weatherManager.delegate = self
    }
    @IBAction func foodPressed(_ sender: UIButton) {
        let allAnnotations = mapView.annotations
        mapView.removeAnnotations(allAnnotations)
        findPlace(place: (sender.titleLabel?.text)!)
        
    }
    
    func findPlace(place: String) {
        localSearch = MKLocalSearch(request: request)
        request.naturalLanguageQuery = place
        request.region = mapView.region
        self.localSearch?.start { (searchResponse, _) in
            guard let response = searchResponse else {
                return
            }
            for mapItem in response.mapItems {
                let place = MKPointAnnotation()
                place.title = mapItem.name
                place.coordinate = mapItem.placemark.coordinate
                self.mapView.addAnnotation(place)
            }
        }
    }
    
}

Upvotes: 0

Views: 27

Answers (1)

Fortaite
Fortaite

Reputation: 1

Figured it out.

localSearch = MKLocalSearch(request: request) was above the region and search criteria statements my code was:

localSearch = MKLocalSearch(request: request)
request.naturalLanguageQuery = place
request.region = mapView.region

Correct code is:

request.naturalLanguageQuery = place
request.region = mapView.region
localSearch = MKLocalSearch(request: request)

Upvotes: 0

Related Questions