2012-04-13 7 views
5

mapviewに複数の注釈(arround 500)を追加したいが、表示される最大値は100である.100を超えた場合、viewForAnnotationデリゲートメソッドが呼び出されない。しかし、それはコードがあり、アノテーションここMKMapViewで描画できる注釈の最大数(MKAnnotation)?

100以下のために完璧に動作します( annotationArrayのカウントが配列未満101の場合のみ動作します)

_allPoints = [[NSMutableArray alloc] init]; 

NSString* responseFile = [[NSBundle mainBundle] pathForResource:@"Datafile" ofType:@"txt"]; 
NSData *sampleData = [NSData dataWithContentsOfFile:responseFile]; 
if (sampleData) { 
    NSError* error; 
    NSDictionary* json = [NSJSONSerialization 
          JSONObjectWithData:sampleData 

          options:kNilOptions 
          error:&error]; 

    NSArray* response = [json objectForKey:@"response"]; 
    for (NSDictionary *lineDict in response) { 
     if ([[lineDict objectForKey:@"data"] isKindOfClass:[NSArray class]]) { 
      SinglePoint *singlePoint = [[SinglePoint alloc] initWithDictionary:lineDict]; 
      [_allPoints addObject:singlePoint]; 
     } 
     else { 
      NSLog(@"Error"); 
     } 
    } 
} 


_mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 
[_mapView setDelegate:self]; 
[self.view addSubview:_mapView]; 

NSMutableArray *annotationArray = [[NSMutableArray alloc] init]; 
for (int i=0; i<[_allPoints count]; i++) { 
    SinglePoint *singlePoint = [_allPoints objectAtIndex:i]; 
    NVPolylineAnnotation *annotation = [[NVPolylineAnnotation alloc] initWithPoint:singlePoint mapView:_mapView]; 
    [annotationArray addObject:annotation]; 
} 
[_mapView addAnnotations:(NSArray *)annotationArray]; 

CLLocationCoordinate2D centerCoord = { 28.632747, 77.219982 }; 
[_mapView setCenterCoordinate:centerCoord zoomLevel:12 animated:NO]; 

デリゲートメソッドは次のとおりです。

編集:コメントごとに、幸運のないビューを再利用し始めました:(

if ([annotation isKindOfClass:[NVPolylineAnnotation class]]) { 
    static NSString *viewIdentifier = @"annotationView"; 
    NVPolylineAnnotationView *annotationView = (NVPolylineAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier:viewIdentifier]; 
    if (annotationView == nil) { 
     annotationView = [[NVPolylineAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:viewIdentifier mapView:_mapView]; 
    } 
    return annotationView; 
} 
return nil; 

ドキュメントや他の場所では何の制限も見つけられませんでした。メモリに問題はありますか?

+1

はどうなりますか? –

+0

'NVPolylineAnnotation'のソースコードを提供することは可能ですか? –

答えて

0

最後に問題を追跡できました。それを解決するには、まずmapViewの中心を設定し、後でアノテーションを追加します。私はまだ100の注釈が以前に表示された理由(そして理由は100だけではない)を知らない。あなたの提案と時間をいただきありがとうございます。

これは私が変更されたコードである: - あなたは `annotationArray`に` allPoints` 500回で最初のオブジェクトを追加する場合

_mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 
[_mapView setDelegate:self]; 
[self.view addSubview:_mapView]; 

CLLocationCoordinate2D centerCoord = { 28.632747, 77.219982 }; 
[_mapView setCenterCoordinate:centerCoord zoomLevel:12 animated:NO]; 

NSMutableArray *annotationArray = [[NSMutableArray alloc] init]; 
for (int i=0; i<[_allPoints count]; i++) { 
    SinglePoint *singlePoint = [_allPoints objectAtIndex:i]; 
    NVPolylineAnnotation *annotation = [[NVPolylineAnnotation alloc] initWithPoint:singlePoint mapView:_mapView]; 
    [annotationArray addObject:annotation]; 
} 
[_mapView addAnnotations:(NSArray *)annotationArray]; 
+0

すべての注釈点を囲む 'MKCoordinateRegion'を構築し、[setRegion:animated:](http://developer.apple.com/library/ios/#)を使用することで、ハードコーディングされた緯度と経度を避けることは可能でしょうか?ドキュメンテーション/ MapKit/Reference/MKMapView_Class/MKMapView/MKMapView.html#// apple_ref/occ/instm/MKMapView/setRegion:animated :)地図を任意の位置に配置してズームする必要がありますか? (ポイントがない場合はフォールバックしてください。例外が発生する場合もあります) – Dondragmer

+0

回答をお寄せいただきありがとうございます。 –

1

MKMapViewは、annotationViewsの制限がありません。しかし、それは一定のビュー数(+1000)を超えるとかなり遅くなり、使用できなくなります。

私はこの理由は、annotationViewの管理が完全に間違っていると考えているからです。 ARCを使用している場合でも、すべての単一注釈に対して一意のビューを作成しないでください。むしろUITableViewのセルのように、未使用のビューをすべて再利用する必要があります。

Introduction to MapKit on iOS - Ray Wenderlichのような、このための良いチュートリアルがいくつかあります。

これで問題が解決しない場合は、アノテーションクラスのデバッグを試みる必要があります。 (NVPolylineAnnotationおよびNVPolylineAnnotationView)。多分何か間違っているかもしれない。 ポイント配列の等価座標もチェックしましたか?

+1

ありがとう@yinkou .. 1)私は101のビューがあるときに正確に起こるので、問題はメモリ管理とは思わない。 2)私は等しい座標の点をチェックしたが、何もない。いくつかの点が同じであっても、残りの100点を描画する必要があります。 – Shri

+0

あなたは正しいですが、それは推奨パターンなのでannotationViewsを再利用する必要があります。 _allPoints配列を作成して作成する場所にコードを投稿できますか? – yinkou

+1

質問を更新しました.. – Shri

0

注釈は、好きなだけ追加できます。ただし、アノテーションのcoordinateプロパティがマップビューの表示部分と交差するまで、各アノテーションのビューは作成されません。注釈をマップに追加しただけで、MapKitはビューを作成しません。

+0

この場合、ビュー数が100を超えたときにのみ描画されます。101ビューを提供する場合、最初の100ビューは、表示されていた以前のビューと表示されているビューです。しかし、101ビューになるとすぐに何も表示されません。デリゲートさえ呼び出されません。 – Shri

関連する問題