I am working with a MKMapView that is placed on the third VC (MapViewController: UIViewController) in a tabbed application controller (AppTabController: UITabViewController). If I understand UITabViewController correctly, the MapViewController will not be instantiated until the user navigates to it.
The MKMapView is linked to the MapViewController via the outlet
@IBOutlet weak var mapView: MKMapView!
MapViewController conforms to the MKMapViewDelegate protocol through an extension to MapViewController and I’ve set the MKMapView delegate in the viewDidLoad method of MapViewController.
extension MapViewController: MKMapViewDelegate {
// delegate methods
}
override func viewDidLoad() {
super.viewDidLoad()
mapView.delegate = self
}
In viewDidLoad of MapViewController I call showMyLocation() to centre the map on the user’s location in a 1 km. by 1 km. region and show their annotation.
The same method is linked to a “User” button on MapViewController’s nav bar which allows the user to return to their location if they want to.
@IBAction func showMyLocation() {
let region = MKCoordinateRegionMakeWithDistance(mapView.userLocation.coordinate, 1000, 1000)
mapView.setRegion(mapView.regionThatFits(region), animated: true)
}
I am not using a CLLocationManager to determine the user’s location. Because MKMapView is able to return the user’s location to the app.
Answers I've found so far point towards a CLLocationManager approach.
I am a little unsure about what appears to be a timing dependency in obtaining the user’s coordinates from MKMapView.
If I start the app and navigate immediately to the MapViewController tab the coordinates returned by mapView.userLocation.coordinate are 0,0
However if I start the app and wait for a minute or so before going to the MapViewController tab the coordinates are the expected ones for my location.
Finally, if I start the app and navigate immediately to the MapViewController the coordinates are 0,0 as above but if I immediately tap the “User” button the coordinates immediately update to the expected ones without the delay.
My solution so far is to implement the didUpdateUserLocation delegate method to tell me when MKMapView has obtained a valid location for the user.
func mapView(mapView: MKMapView, didUpdateUserLocation userLocation: MKUserLocation) {
if self.rideLocations.isEmpty {
self.showUserLocation()
}
}
My question is have I implemented the necessary pattern to deal with MKMapView user location behaviour timing?
My objective is to have the user’s location properly displayed on the MKMapView regardless of when they navigate to the tab that has the map on it but to avoid overuse of the device location services.
Thanks for all the answers so far - they’ve been a massive help in my Swift / IOS journey!
didUpdateUserLocation
, you can check a stored long/lat value ofuserLocation
against the new values. You can do your logic if the difference is more than 1 or 10 or 100 or whatever value would work for you. Don't forget to store the new values as the stored values. – GlennRay Oct 22 '15 at 22:53