I am doing a project that is like this.
Have a text box/button that takes a zip code and pass the zip code to a segue to the map view controller that shows the zip code region on the map. The entire setup is behind a navigation controller so that I could go back and try another zip code in the previous view.
The problem is that the map view plots the zip code properly the first time, but then when I go back and enter another zip code in the previous view controller, the map view doesn't set the region to the new zip code properly, instead it shows a new map view with the default region in the simulator. See the below code
class MapViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
@IBOutlet var mapView: MKMapView!
var searchZipCode:String! = "95110" //used by other segues to set zipcode to show on this page
let locationManager = CLLocationManager()
let geocoder = CLGeocoder()
var dispatchGroup = dispatch_group_create()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
self.mapView.showsUserLocation = true
dispatchGroup = dispatch_group_create()
dispatch_group_enter(dispatchGroup)
self.plotZipCodeInMap(self.searchZipCode)
dispatch_group_notify(dispatchGroup, dispatch_get_main_queue()){
print("Zip code plotted.")
}
func plotZipCodeInMap(zipCode:String) {
geocoder.geocodeAddressString(zipCode, completionHandler: {(placemarks, error) -> Void in
print("Zip code = \(zipCode)")
if let placemark = placemarks?.first {
print("Successfully decoded the zip code..")
let coordinate = placemark.location?.coordinate
let center = CLLocationCoordinate2D(latitude: coordinate!.latitude, longitude: coordinate!.longitude)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.04, longitudeDelta: 0.04))
self.mapView.setRegion(region, animated: true)
dispatch_group_leave(self.dispatchGroup)
}
})
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print("Location updated..")
let locations = locations.last
let center = CLLocationCoordinate2D(latitude: locations!.coordinate.latitude, longitude: locations!.coordinate.longitude)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.075, longitudeDelta: 0.075))
self.mapView.setRegion(region, animated: true)
self.locationManager.stopUpdatingLocation()
}
}
The problem is when I go back to the first view in the navigation controller and hit another zip code, the map view seems to be entering the plotZipCodeInMap method properly, but then it doesn't print the line
"Successfully decoded the zip code.."
which means there is some trouble in getting the placemark the second time. Not sure what could be the issue here. The variables are all local and I expect them to behave no differently when clicking back button (the map view disappears) and the view is shown again with another zip code set based on my input.