Reputation: 119
I want to zoom the map to the current user location at startup. I tried to retrive the user location using mapView.userLocation.coordinate, at viewDidLoad, but the returned coordinate was (0,0), probably because the MapKit doen't "find" the user location at startup.
I found a solution implementing the method didUpdateToLocation. I did the following:
- (void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
if ( hasZoomedAtStartUp == NO )
{
[self zoomAtStartUp]; // my method to zoom the map
hasZoomedAtStartUp = YES;
}
}
The hasZoomedAtStartUp variable I created on the .h file and initialied it with NO in the ViewDidLoad.
This solution works fine, but I would like to know if there is another way to do that, without the if statement. That IF is relevant justo for the startUp, so I want to remove it, for performance reasons.
Upvotes: 2
Views: 3374
Reputation: 550
You can make an 'initial' delegate implementation,that you can deregister after zooming to location, and register your 'normal' delegate that now do not need the whole zoom and if at all.
Upvotes: 2
Reputation: 6067
You can initialise and begin getting location updates whenever you like. The CLLocationManager will notify your delegate whenever a new location is received and set that Location on map Region to Display
//Don't Forget To Adopt CLLocationManagerDelegate protocol
//set up the Location manager
locationManager = [[CLLocationManager alloc] init];
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = DISTANCE_FILTER_VALUE;
locationManager.delegate = self;
[locationManager startUpdatingLocation]
//WIll help to get CurrentLocation implement the CLLocationManager delegate
- (void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
// use this newLocation .coordinate.latitude
}
// set Span
MKCoordinateSpan span;
//You can set span for how much Zoom to be display like below
span.latitudeDelta=.005;
span.longitudeDelta=.005;
//set Region to be display on MKMapView
MKCoordinateRegion cordinateRegion;
cordinateRegion.center=latAndLongLocation.coordinate;
//latAndLongLocation coordinates should be your current location to be display
cordinateRegion.span=span;
//set That Region mapView
[mapView setRegion:cordinateRegion animated:YES];
Upvotes: 2
Reputation: 113757
The method you're using now, -locationManager:didUpdateToLocation:fromLocation
is the best place to do anything with user location. There are a couple of things that I'd do different from you though.
First of all, you accept the very first location update as the best. You've probably asked for a certain accuracy, but asking for it doesn't mean that the newLocation
from that method is the best. Often, you'll get a very low accuracy, or a cached location from some time in the past. What I'd do is to check the new location for age and accuracy, and only when it's good zoom in.
The other thing I'd do is to turn off location updates, either when an update with good accuracy came in, or 30 seconds after updates start. Set a timer to turn it off, and when you turn it off, set a longer timer to turn it back on and check again.
Lastly, make sure that you've properly implemented the -locationManager:didFailWithError:
for all cases. It's always one of the things tested when you submit an app. If it doesn't fail gracefully (for example, in Airplane mode) it will probably get rejected.
Search around Stack Overflow for techniques and code to do these things.
Upvotes: 2
Reputation: 9860
I highly doubt a failing if statement is something you need to worry about for performance.
Do you need location services constantly? If not, you'd probably have much larger gains from calling stopUpdatingLocation when you no longer need the location updated. Subsequently, you won't even reach didUpdateToLocation since you're no longer getting new location data.
Upvotes: 5