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 'NSInvalidArgumentException', reason: 'Invalid Region '" when the view loads. Please can someone help?
Cheers!
- (void)viewDidLoad
{
[super viewDidLoad];
MKCoordinateRegion mapRegion;
mapRegion.center.latitude = map.userLocation.coordinate.latitude;
mapRegion.center.longitude = map.userLocation.coordinate.longitude;
mapRegion.span.latitudeDelta = 0.2;
mapRegion.span.longitudeDelta = 0.2;
[map setRegion:mapRegion animated: YES];
}
Here is @Deepak's answer in Swift 3 for iOS 10:
As with Deepak's answer, except you could set the span more elegantly:
did you set
showsUserLocation = YES
?MKMapView
won't update the location if it is set toNO
. So make sure of that.It is very likely that the
MKMapView
object doesn't have the user location yet. To do right, you should adoptMKMapViewDelegate
protocol and implementmapView:didUpdateUserLocation:
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.
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 :this way you could display your location with the zoom that you want. I hope this will help.
......