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.coor开发者_如何学Pythondinate;
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 to startUpdatingLocation
:
didZoomToUserLocation = NO;
[locationManager startUpdatingLocation];
Then in didUpdateToLocation
, change the code like this:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
location = newLocation.coordinate;
if (didZoomToUserLocation)
return;
didZoomToUserLocation = YES;
MKCoordinateRegion region;
region.center = location;
MKCoordinateSpan span;
span.latitudeDelta = .005;
span.longitudeDelta = .005;
region.span = span;
[mapView setRegion:region animated:TRUE];
}
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:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
location = newLocation.coordinate;
if (didZoomToUserLocation)
{
//just re-center map on user's location without changing zoom...
[mapView setCenterCoordinate:newLocation.coordinate animated:YES];
}
else
{
didZoomToUserLocation = YES;
MKCoordinateRegion region;
region.center = location;
MKCoordinateSpan span;
span.latitudeDelta = .005;
span.longitudeDelta = .005;
region.span = span;
[mapView setRegion:region animated:TRUE];
}
}
Also, in your viewDidLoad, you don't need to call insertSubview on the mapView if it's created in IB.
Your code in didUpdateToLocation sets a span of a fixed value. Every time that delegate call is invoked it will set the zoom based on those .005 spans.
If you simply put a breakpoint or log in the function you'll see it generally gets called quite frequently.
精彩评论