【问题标题】:MKMapView application crashes after zoom is used使用缩放后 MKMapView 应用程序崩溃
【发布时间】:2012-07-09 09:31:15
【问题描述】:

首先,MKMapView 中只有用户位置。经过一些操作后,我调用了方法: [self mapView:self.mapView didAddAnnotationViews:self.pointersArray]; 我的didAddAnnotationViews 方法:

-(void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
{
    if (views.count == 1) {
        MKAnnotationView *annotationView = [views objectAtIndex:0];
        id<MKAnnotation>mp = [annotationView annotation];
        MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance([mp coordinate], 500, 500);
        [mapView setRegion:region animated:YES]; 
    }
    else {
        [mapView addAnnotations:views];
    } 
}

在不使用缩放之前,应用程序不会崩溃。但是当缩放使用超过 10 次(大约)时,我会在这个 [mapView addAnnotations:views]; 或有时在 return UIApplicationMain(argc, argv, nil, NSStringFromClass([BIDAppDelegate class])); 中出错。错误 - EXC_BAD_ACCESS。有我的问题吗?

编辑

更改为[self.mapView setRegion:region animated:YES]; 但现在我在主线程MKNormalizedPointForLayer EXC_BAD_ACCESS 中有错误。通常缩放工作正常,应用程序在使用缩放 7 次或更多次后崩溃。 我的按钮操作:

- (void)showKantorsOnMap {
    if (self.kantorsData.count != self.pointersArray.count) {
        NSLog(@"need to wait more");
    }
    NSMutableArray *toRemove = [[NSMutableArray alloc] init];
    for (id annotation in self.mapView.annotations)
    if (annotation != self.mapView.userLocation)
        [toRemove addObject:annotation];
    [self.mapView removeAnnotations:toRemove];
    [self.mapView addAnnotations:self.pointersArray];
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(self.mapView.userLocation.coordinate,6500, 6500);
    [self.mapView setRegion:region animated:YES]; 
}

解决方案 问题在于 didAddAnnotationViews 方法 [mapView addAnnotations:views]; 调用递归。

【问题讨论】:

    标签: iphone objective-c ios mkmapview mapkit


    【解决方案1】:

    首先,您不应该自己调用didAddAnnotationViews 委托方法。地图视图本身将在实际显示您添加到地图的注释后调用它(使用addAnnotationaddAnnotations)。

    其次,在那个方法中这一行:

    [mapView addAnnotations:views];
    

    至少有两个原因是错误的:

    • views 参数是MKAnnotationViewsNSArray不是 id&lt;MKAnnotation&gt; 对象)。 addAnnotations 方法需要 NSArrayid&lt;MKAnnotation&gt; 对象。
    • didAddAnnotationViews 委托方法中调用addAnnotations 可能不是一个好主意(可能会导致递归调用委托方法导致堆栈溢出)

    最后,如果您想缩放地图以显示所有注释,已经有许多可能的解决方案的答案(例如,搜索“mkmapview annotations region fit zoom”或类似的东西)。以下是您可以尝试的一些答案:

    基本上,循环遍历mapView.annotations 数组以找到最小和最大纬度/经度值。然后从中创建一个MKCoordinateRegion(中心是中点,增量是差异)。然后拨打setRegion

    【讨论】:

    • 对不起,基于你的昵称:)。我编辑了我的问题。也许还需要一些我的代码?
    • setRegion 是从哪里调用的?还在直接调用委托方法吗?您是否添加任何注释,如果是,在哪里以及如何添加?
    • setRegion 在我的视图加载(在委托方法中)和按钮的操作中调用。
    • 检查您添加的任何注释的坐标是否有效(纬度在 -90 到 90 的范围内,经度在 -180 到 180 的范围内)。这就是 this question 声称的 MKNormalizedPointForLayer 错误的原因。另外,您是否在后台线程中做任何与地图相关的工作?
    • 坐标没问题。为了获取我的注释坐标,我使用了 google api。
    【解决方案2】:

    您可以尝试这些代码,而不是使用上面的代码。这对我有用...

    在 NSObject 下创建新类并命名为 MapClass 在 mapclass.h 中

     #import <UIKit/UIKit.h>
     #import <MapKit/MapKit.h>
    
    @interface MapClass : NSObject <MKAnnotation>{
    CLLocationCoordinate2D coordinate;
    NSString *title;
    NSString *subtitle;
    }
    @property (nonatomic, assign) CLLocationCoordinate2D coordinate;
    @property (nonatomic, copy) NSString *title;
    @property (nonatomic, copy) NSString *subtitle; 
    @end
    

    在 MapClass.m 文件中

    #import "MapClass.h"
    
    @implementation MapClass
    @synthesize coordinate,title,subtitle;
    @end
    

    将其插入 .h 文件中

    #import <MapKit/MapKit.h>
    @interface MapViewController : UIViewController
    
    {
    
    MKMapView *mapview;
    
    }
    
    @property (nonatomic, retain) IBOutlet MKMapView *mapview;
    @end
    

    将其插入 .m 文件中

    [mapview setMapType:MKMapTypeStandard];
        [mapview setZoomEnabled:YES];
        [mapview setScrollEnabled:YES];
    
        MKCoordinateRegion region = { {0.0, 0.0 }, {0.0, 0.0 } };
        region.center.latitude = xxx;//your longitude
        region.center.longitude = xxx;//your latitude
        region.span.longitudeDelta = 0.01f;
        region.span.latitudeDelta = 0.01f;
        [mapview setRegion:region animated:YES];
    
        MapClass *ann = [[MapClass alloc] init];
        ann.title = @"Title";
        ann.subtitle = @"Subtitle.";
        ann.coordinate = region.center;
        [mapview addAnnotation:ann];
    

    //mapview 是 .h 文件中声明的 MKmapview 的变量。

    【讨论】:

    • 不幸的是错误并没有消失。 MKNormalizedPointForLayer主线程出错
    • 你在NSObject下创建了mapclass吗?
    猜你喜欢
    • 2011-08-30
    • 1970-01-01
    • 2012-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多