开发者

Auto zoom in resets when the user zooms out

I made a Map to show the user location with an auto zoom in, however when the user try to zoom out, it was zooming in automtically which is not practical,can you help me to disable the auto zoom in when the user zoom out ?. Here is part of my code :

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

}

Edited: I added this statement in the viewDidLoad:

mapView.zoomEnabled=FALSE;

should it disable the auto zoom in when the user zoom out ?

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


    }

Edit: I'm still working on this, so before continuing, I want to show you my logic and I'm waiting for your advices :) So, in my view which is charged to show me user location on the map, added a BOOLEAN variables to test if the user has adjusted the zoom or not. .h

BOOL shouldAdjustZoom;

and for the methods :

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

.m

I added the i开发者_运维问答mplementation of the getter -(BOOL)shouldAdjustZoom so this getter will call the zoomLevelForMapRect to know if the zoom level was changed or not.

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

}

What I need to know is how should I call the zoomLevelForMapRect method, it's with parameters, in the getter I need to call it :

 -(BOOL)shouldAdjustZoom
        {

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

        }


There is no such thing as Auto-Zoom. You set the zoom level manually by defining the span as you have done:

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

Setting the span manually will always show a fixed zoom level.

If you want to keep the current zoom level, do this:

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

Consider using a flag to alternate between your desired behavior.

I.e.

on viewDidLoad set _shouldAdjustZoom = YES; Then when the user adjusts the zoom set _shouldAdjustZoom = NO; (there is a delegate method of the map view called when the user changes the zoom)

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜