My objective is to make a map in which the user location is shown with annotation and zoom, everything is ok, the location is there with a red annotation and a pretty zoom, for that I have a view which is called PositionActuelleViewController, here is my code :
PositionActuelleViewController.h :
@interface PositionActuelleViewController : UIViewController<MKMapViewDelegate,CLLocationManagerDelegate> {
MKMapView *mapView;
MKReverseGeocoder *geoCoder;
MKPlacemark *mPlacemark;
CLLocationCoordinate2D location;
}
@property (nonatomic,retain)IBOutlet MKMapView *mapView;
@end
PositionActuelleViewController.m :
- (void)viewDidLoad {
[super viewDidLoad];
[mapView setShowsUserLocation:TRUE];
[mapView setMapType:MKMapTypeStandard];
[mapView setDelegate:self];
[self.view insertSubview:mapView atIndex:0];
CLLocationManager *locationManager=[[CLLocationManager alloc] init];
[locationManager setDelegate:self];
[locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
[locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
location = newLocation.coordinate;
MKCoordinateRegion region;
region.center = location;
MKCoordinateSpan span;
span.latitudeDelta = .005;
span.longitudeDelta = .005;
region.span = span;
[mapView setRegion:region animated:TRUE];
}
My only problem is that the zoom in is always enabled even if the user zoom out the map, it zoomed in automatically. How can I fix this?
If you only want to zoom in once, you can add a boolean ivar called didZoomToUserLocation for example.
In
viewDidLoad, initialize it to NO before the call tostartUpdatingLocation:Then in
didUpdateToLocation, change the code like this:Note that this will also stop following the user on the map (but the location ivar will still be updated).
If you want to keep following the user but zoom in only the first time, then do this instead:
Also, in your viewDidLoad, you don’t need to call insertSubview on the mapView if it’s created in IB.