ここでできる最大の2つの速度改善は次のとおりです。
- アノテーションビューの再利用を実装する(今は同じビューが再び表示されてもアノテーションを表示する必要があるたびに新しいビューを作成します)。
-
UniqueID
の設定方法を変更します。これを設定するために、コードは現在、注釈ビューを作成するたびにすべての注釈をループしています(最初の時間だけでなく、マップビューがズームまたはスクロールされるたびに発生する可能性があります)。
まず、 viewForAnnotation
メソッドの UniqueID
を検索し、ボタンタグを使用して注釈識別子を渡す代わりに、 UniqueID
をアノテーション自体を addPins
に追加すると、カスタムアノテーションクラスのプロパティ MyLocation
annotation.uniqueID = info.UniqueID; //<-- give id to annotation itself
[mapViewLink addAnnotation:annotation];
別々にプロパティを割り当てるのではなく、 initWithName
メソッドに uniqueID
をパラメータとして追加することもできます。
Next, to implement annotation view re-use, the viewForAnnotation
method should look like this:
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id ) annotation{
if (annotation == mapView.userLocation){
return nil; //default to blue dot
}
NSString *reuseId = @"StandardPin";
MKPinAnnotationView *annView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseId];
if (annView == nil)
{
annView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId] autorelease];
annView.pinColor = MKPinAnnotationColorRed;
annView.animatesDrop = YES;
annView.canShowCallout = YES;
annView.calloutOffset = CGPointMake(-5, 5);
UIButton *advertButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
advertButton.frame = CGRectMake(0, 0, 23, 23);
advertButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
advertButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
annView.rightCalloutAccessoryView = advertButton;
}
else
{
//update the annotation property if view is being re-used...
annView.annotation = annotation;
}
return annView;
}
Finally, to respond to the button press and figure out which UniqueID
to show the detail for, implement the calloutAccessoryControlTapped
delegate method:
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view
calloutAccessoryControlTapped:(UIControl *)control
{
MyLocation *myLoc = (MyLocation *)view.annotation;
int uniqueID = myLoc.uniqueID;
NSLog(@"calloutAccessoryControlTapped, uid = %d", uniqueID);
//create, init, and show the detail view controller here...
}
After all these changes, only the initial loading of the annotations will take up most of the time. If that is still a problem, one solution is to only add annotations that would be visible in the currently displayed region and add/remove annotations as the user changes the visible region.