缩放自动返回其初始缩放级别

时间:2011-04-12 19:06:44

标签: ios mapkit

我的目标是制作一个地图,其中显示用户位置带有注释和缩放,一切正常,位置有红色注释和漂亮的缩放,因为我有一个名为 PositionActuelleViewController ,这是我的代码:

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];

}

我唯一的问题是即使用户缩小地图也会始终启用放大,但它会自动放大。我该如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

如果您只想放大一次,可以添加一个名为didZoomToUserLocation的布尔值ivar。

viewDidLoad中,在调用startUpdatingLocation之前将其初始化为NO:

didZoomToUserLocation = NO;
[locationManager startUpdatingLocation];

然后在didUpdateToLocation中,更改代码如下:

- (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];    
}

请注意,这也将停止在地图上关注用户(但仍会更新位置ivar。)

如果您想继续关注用户,但只是第一次放大,那么请改为:

- (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];
    }        
}

此外,在viewDidLoad中,如果在IB中创建了insertSubview,则无需在mapView上调用insertSubview。

答案 1 :(得分:0)

您在didUpdateToLocation中的代码设置固定值的范围。每次调用该委托调用时,它都会根据这些.005跨度设置缩放。

如果您只是设置断点或登录该功能,您会发现它通常会被频繁调用。