1

まず第一に、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];

4

2 に答える 2

2

didAddAnnotationViewsまず、デリゲート メソッドを自分で呼び出すべきではありません。addAnnotationマップ ビュー自体は、(またはを使用して) マップに追加した注釈を実際に表示した後、それを呼び出しますaddAnnotations

第二に、そのメソッドでこの行:

[mapView addAnnotations:views];

少なくとも 2 つの理由で間違っています。

  • viewsパラメータはNSArrayof MKAnnotationViews(オブジェクトではありません)です id<MKAnnotation>このメソッドは、オブジェクトの をaddAnnotations想定しています。NSArrayid<MKAnnotation>
  • addAnnotationsデリゲート メソッド内での呼び出しdidAddAnnotationViewsは、おそらくお勧めできません (デリゲート メソッドが再帰的に呼び出され、スタック オーバーフローが発生する可能性があります)。

最後に、すべての注釈が表示されるようにマップをズームしたい場合は、可能な解決策を備えた多くの回答が既にあります (たとえば、「mkmapview 注釈領域フィット ズーム」などを検索してください)。あなたが試すことができる答えのいくつかはここにあります:

基本的に、配列をループして、mapView.annotations緯度/経度の最小値と最大値を見つけます。次に、MKCoordinateRegionそこから を作成します (中心は中点で、デルタは差です)。次に、 を呼び出しますsetRegion

于 2012-07-09T14:20:03.620 に答える
2

上記のコードを使用する代わりに、これらのコードを試すことができます..これは私にとってはうまくいきました...

NSObject の下に新しいクラスを作成し、mapclass.h で MapClass という名前を付けます。

 #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 の変数です。

于 2012-07-09T10:41:15.473 に答える