用户缩小时自动放大重置

时间:2011-04-09 20:45:27

标签: iphone

我创建了一个地图以显示自动放大的用户位置,但是当用户尝试缩小时,它会自动放大,这是不切实际的,你可以帮我禁用自动放大当用户缩小?。这是我的代码的一部分:

    - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
        // store new user location
        location = newLocation.coordinate;
        //move the map to the new user location
        MKCoordinateRegion region;
        region.center = location;
        // zoom level
        MKCoordinateSpan span;
        span.latitudeDelta = .005;
        span.longitudeDelta = .005;
        region.span = span;
        // apply new coordinates
        [mapView setRegion:region animated:TRUE];

}

编辑: 我在viewDidLoad中添加了这个语句:

mapView.zoomEnabled=FALSE;

是否应该在用户缩小时禁用自动放大?

- (void)viewDidLoad {
    [super viewDidLoad];

    //
    // On veut afficher la position courante de l'utilisateur
    [mapView setShowsUserLocation:TRUE];
    // MKMapTypeStandard est un mode d'affichage parmis 3 disponibles
    // (les deux autres sont MKMapTypeSatelitte et MKMapTypeHybrid et MKMapTypeStandard)
    [mapView setMapType:MKMapTypeHybrid];
    // Le fait de setter le Delegate permet d'appeler méthodes implémentées dans cette classe
    [mapView setDelegate:self];
    // On ajoute la View du mapView a la View du controlleur courant afin de faire afficher la carte
    [self.view insertSubview:mapView atIndex:0];

    // CLLocationManager permet la gestion de la position géographique de l'utilisateur
    CLLocationManager *locationManager=[[CLLocationManager alloc] init];
    // Le fait de setter le Delegate permet d'appeler méthodes implémentées dans cette classe
    [locationManager setDelegate:self];
    // Définit l'échelle de distance à prendre en compte pour le raffraichissement de la position courante
    [locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];

    [locationManager startUpdatingLocation];
    mapView.zoomEnabled=FALSE;


    }

修改 我还在努力,所以在继续之前,我想告诉你我的逻辑,我在等你的建议:) 因此,在我的视图中,向我显示用户在地图上的位置,添加了一个BOOLEAN变量来测试用户是否调整了缩放。 的·H

BOOL shouldAdjustZoom;

以及方法:

-(void)zoomLevelForMapRect:(MKMapRect)mRect withMapViewSizeInPixels:(CGSize)viewSizeInPixels;
-(void)setShouldAdjustZoom:(BOOL)shouldAdjZoom;
-(BOOL)shouldAdjustZoom;

的.m

我添加了getter -(BOOL)shouldAdjustZoom的实现,因此此getter将调用zoomLevelForMapRect以了解缩放级别是否已更改。

-(BOOL)shouldAdjustZoom
{

    [self zoomLevelForMapRect];
    return shouldAdjustZoom;

}
-(void)zoomLevelForMapRect:(MKMapRect)mRect withMapViewSizeInPixels:(CGSize)viewSizeInPixels    {


    NSUInteger zoomLevel=20;
    MKZoomScale zoomScale=mRect.size.width/viewSizeInPixels.width;
    double zoomExponent=log2(zoomScale);
    zoomLevel=(NSUInteger)(20-ceil(zoomExponent));
    if(zoomLevel<20)
    {

        [self setShouldAdjustZoom:NO];


    }



}


- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{

    location = newLocation.coordinate;

    MKCoordinateRegion region;
    region.center = location;

    if ([self shouldAdjustZoom]==YES) {
        // Use the manually defined span
        MKCoordinateSpan span;
        span.latitudeDelta = .005;
        span.longitudeDelta = .005;
        region.span = span;
    } else {
        // Keep the current span
        MKCoordinateRegion mapRegion = mapView.region;  // To get the current span
        mapRegion.center = newLocation.coordinate;
        mapView.region = mapRegion;
    }

    MKCoordinateSpan span;

    span.latitudeDelta = .005;
    span.longitudeDelta = .005;
    region.span = span;
    [mapView setRegion:region animated:TRUE];

}

我需要知道的是我应该如何调用zoomLevelForMapRect方法,它是带参数的,我需要调用它的getter:

 -(BOOL)shouldAdjustZoom
        {

            [self zoomLevelForMapRect];//how should I fix the call??
            return shouldAdjustZoom;

        }

1 个答案:

答案 0 :(得分:2)

没有自动缩放这样的功能。您可以通过定义跨度来手动设置缩放级别:

span.latitudeDelta = .005;
span.longitudeDelta = .005;

手动设置范围将始终显示固定的缩放级别。

如果要保持当前缩放级别,请执行以下操作:

MKCoordinateRegion mapRegion = mapView.region;  // To get the current span
mapRegion.center = newLocation.coordinate;
mapView.region = mapRegion;

考虑使用标志在您期望的行为之间切换。

即。

on viewDidLoad set _shouldAdjustZoom = YES;然后当用户调整缩放集_shouldAdjustZoom = NO;时(当用户更改缩放时调用地图视图的委托方法)

 (void)locationManager:(CLLocationManager *)manager 
   didUpdateToLocation:(CLLocation *)newLocation 
          fromLocation:(CLLocation *)oldLocation 
{
     if (_shouldAdjustZoom) {
         // Use the manually defined span
     } else {
         // Keep the current span
     }
}