2012-07-09 29 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