Reputation: 4813
I have the following code in place:
-(void)viewDidLoad
{
//Set Zoom level using Span
MKCoordinateSpan span;
span.latitudeDelta = 0.05;
span.longitudeDelta = 0.05;
region.span = span;
}
-(void)locationChange:(CLLocation *)newLocation: (CLLocation *)oldLocation
{
// This zooms in on the users current loation.
curlocation = newLocation.coordinate;
region.center = curlocation;
[_mapView setRegion:region animated:TRUE];
}
Initially the zoom level is set as per the code in ViewDidLoad. How do I store the zoom level id the user zooms in or out, as everytime a new location update is received the zoom level is reset.
Is there a way of detecting the user has zoomed in or out ?
UPDATE
I've add the regionDidChangeAnimated method as follows:
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
NSLog(@"Region DID change. Center is now %f,%f, Deltas=%f,%f",
region.center.latitude, region.center.longitude,
region.span.latitudeDelta, region.span.longitudeDelta);
}
Output in the log looks like:
2010-11-01 15:17:29.317 Legginit[2948:307] Region DID change.
Center is now 54.181150,-8.483177, Deltas=0.050000,0.050000
2010-11-01 15:17:30.553 Legginit[2948:307] Region DID change.
Center is now 54.181150,-8.483177, Deltas=0.050000,0.050000
2010-11-01 15:17:31.063 Legginit[2948:307] Region DID change.
Center is now 54.181150,-8.483177, Deltas=0.050000,0.050000
2010-11-01 15:17:31.653 Legginit[2948:307] Region DID change.
Center is now 54.181150,-8.483177, Deltas=0.050000,0.050000
2010-11-01 15:17:32.582 Legginit[2948:307] Region DID change.
Center is now 54.181150,-8.483177, Deltas=0.050000,0.050000
2010-11-01 15:17:33.608 Legginit[2948:307] Region DID change.
Center is now 54.181150,-8.483177, Deltas=0.050000,0.050000
As I zoom in on the phone I was expecting the Delta Values to change but they remain at 0.05. Am I misunderstanding how this works. I thought I could capture the Delta values and store them so I could reset the zoom level if the user exits and re-enters the map.
Regards, Stephen
Upvotes: 1
Views: 2604
Reputation: 43
Instead of using the region, just change the center coordinate. I've found that setting the region itself can change the zoom level slightly, even when telling the mapView to use the same region as the current mapView.
[_mapView setCenterCoordinate:newLocation.coordinate animated:YES];
Upvotes: 0
Reputation:
In locationChange, try updating your region's span to the _mapView's current span before calling setRegion:
region.span = _mapView.region.span;
If you really need to detect zoom changes as they happen, implement the MKMapViewDelegate method regionWillChangeAnimated or regionDidChangeAnimated.
Edit:
You need to get the new region from the mapView and set your region instance variable to it (the map view won't set your instance variable automatically):
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
region = mapView.region; //<-- add this
NSLog(@"Region DID change. Center is now %f,%f, Deltas=%f,%f",
region.center.latitude, region.center.longitude,
region.span.latitudeDelta, region.span.longitudeDelta);
}
Upvotes: 2