iphone - 当用户缩小时自动放大重置

标签 iphone

我制作了一个 map 来显示用户位置并自动放大,但是当用户尝试缩小时,它会自动放大,这是不切实际的,你能帮我禁用自动放大吗当用户缩小时?。这是我的代码的一部分:

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


    }

编辑: 我仍在研究这个问题,所以在继续之前,我想向您展示我的逻辑,并且我正在等待您的建议:) 因此,在我看来,它负责在 map 上显示用户位置,添加了一个 bool 变量来测试用户是否调整了缩放。 .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;

        }

最佳答案

不存在自动缩放这样的东西。您可以通过定义跨度来手动设置缩放级别:

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;(当用户调整缩放比例时,会调用 map View 的委托(delegate)方法更改缩放)

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

关于iphone - 当用户缩小时自动放大重置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5607784/

相关文章:

iphone - 测试 NSURLConnection 与 HTTP 响应错误状态的使用

ios - 为什么 Safari 在使用 iOS 6 设备进行远程调试时显示 "No Inspectable Applications"?

iphone - 如何禁用 NSDictionary 自动按键排序

iphone - iPhone核心动画推荐阅读

iphone - 如何在 UISearchBar 中关闭实时搜索

ios - 检测图像是否已加载到 UIWebView 中?

iphone - iPad 应用程序的奇怪崩溃报告

iphone - 如何在 iPhone 上以编程方式显示邮件设置页面?

javascript - requirejs 需要文字!相对路径在 iphone 上导致非规范化错误,bet 在模拟器上工作

ios - 如何使用 iPhone 传感器数据计算 GPS DoP(精度因子)?