I\'m trying to zoom a map into the user\'s current location once the view loads, but I\'m getting the error \"** * Terminating app due to uncaught exception \'NSInvalidA
The problem that occurs when you use the didUpdateUserLocation
method, you will not be able to scroll nor zoom to another location. It will keep pointing to your location.
The best way (that I've tried) is that you need to use the location manager.
In the viewDidLoad
add the following :
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.distanceFilter = kCLLocationAccuracyKilometer;;
// showing user location with the blue dot
[self.mapView setShowsUserLocation:YES];
self.mapView.delegate = self;
// getting user coordinates
CLLocation *location = [_locationManager location];
CLLocationCoordinate2D coordinate = [location coordinate];
// showing them in the mapView
_mapView.region = MKCoordinateRegionMakeWithDistance(coordinate, 250, 250);
[self.locationManager startUpdatingLocation];
this way you could display your location with the zoom that you want. I hope this will help.
Swift 3 version Use this method to zoom into any coordinate.
extension MKMapView{
func zoomIn(coordinate: CLLocationCoordinate2D, withLevel level:CLLocationDistance = 10000){
let camera =
MKMapCamera(lookingAtCenter: coordinate, fromEyeCoordinate: coordinate, eyeAltitude: level)
self.setCamera(camera, animated: true)
}
}
Here is @Deepak's answer in Swift 3 for iOS 10:
extension ViewController: MKMapViewDelegate
{
func mapView( _ mapView: MKMapView, didUpdate userLocation: MKUserLocation )
{
let regionRadius = 400 // in meters
let coordinateRegion = MKCoordinateRegionMakeWithDistance( userLocation.coordinate, regionRadius, regionRadius )
self.mapView.setRegion( coordinateRegion, animated: true)
}
}
Check the value of map.userLocation.coordinate
, the rest is OK.
You don't want to update this stuff inside userDidUpdateLocation if there is the possbility that the user will want to scroll the map. If you put that code in the mentioned method, the user will not be able to scroll the map because the function will be called and center the map back to the current location.
By far the easiest way is to use mapView.showAnnotations in didUpdateUserLocation:
func mapView(mapView: MKMapView!, didUpdateUserLocation userLocation: MKUserLocation!) {
mapView.showAnnotations([userLocation], animated: true)
}
That's all!
MKUserLocation conforms to the MKAnnotation protocol. Pass the userLocation as an array, the showAnnotations method will let the mapView zoom in on a region plus padding spanning the array of MKAnnotations,in this case it's just the userLocation. Works for me.
If you only want to zoom in once, use a initialUserLocation property to check whether the initial property was already set. I don't like using dispatch_once at all for that sort of thing