2012-07-09 110 views
1

首先在MKMapView中只有用户位置。一些行动后,我打电话方法: [self mapView:self.mapView didAddAnnotationViews:self.pointersArray];didAddAnnotationViews方法:MKMapView应用程序在使用缩放后崩溃

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

SOLUTION 问题是didAddAnnotationViews方法[mapView addAnnotations:views];称为递归。

回答

2

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

其次,在该方法中这行:

[mapView addAnnotations:views]; 

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

  • views参数是MKAnnotationViewsid<MKAnnotation>对象)的NSArrayaddAnnotations方法预计NSArrayid<MKAnnotation>对象。
  • didAddAnnotationViews委托方法中调用addAnnotations可能不是一个好主意(可能会导致递归调用的委托方法导致堆栈溢出)

最后,如果你想缩放地图,使其显示所有的注释,有很多答案已经可能的解决方案(例如,搜索“mkmapview注释区域适合缩放”或类似的东西)。这里有几个问题的答案,你可以尝试:

基本上,遍历mapView.annotations阵列发现的最小和最大纬度/经度值。然后从中创建一个MKCoordinateRegion(中心是中点,三角洲是差异)。然后致电setRegion

+0

你会说俄语吗?) – RomanHouse 2012-07-09 15:16:12

+0

不,不幸的不是。 – Anna 2012-07-09 15:29:01

+0

对不起,基于您的昵称:)。我编辑了我的问题。也许需要另一个我的代码? – RomanHouse 2012-07-09 15:37:03

2

而不是使用上面的代码,你可以尝试这些codes..This工作对我来说...

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。

+0

不幸的是,错误并没有消失。 'MKNormalizedPointForLayer'中主线程出错 – RomanHouse 2012-07-09 11:00:59

+0

您是否在您的应用中包含了mapkit框架? – Dany 2012-07-09 11:05:14

+0

当然可以。 – RomanHouse 2012-07-09 11:07:02