私のアプリには、UILongPressGestureRecognizerでピンをドロップするmapViewがあります。画面を押すと、座標がジオコーディングされてピンがドロップされ、アドレスがタイトルとして設定されます。画面には複数のピンがあり、注釈ビューには、PinViewControllerと呼ばれる別のコントローラーをスタックにプッシュするコールアウトアクセサリがあります。PinViewControllerには、ピンのタイトルを表示したいラベルがあります。
ピンをドロップするためのコードは次のとおりです。
-(void)press:(UILongPressGestureRecognizer *)recognizer
{
CGPoint touchPoint = [recognizer locationInView:worldView];
CLLocationCoordinate2D touchMapCoordinate = [worldView convertPoint:touchPoint toCoordinateFromView:worldView];
geocoder = [[CLGeocoder alloc]init];
CLLocation *location = [[CLLocation alloc]initWithCoordinate:touchMapCoordinate
altitude:CLLocationDistanceMax
horizontalAccuracy:kCLLocationAccuracyBest
verticalAccuracy:kCLLocationAccuracyBest
timestamp:[NSDate date]];
[geocoder reverseGeocodeLocation:location
completionHandler:^(NSArray *placemarks, NSError *error) {
NSLog(@"reverseGeocoder:completionHandler: called");
if (error) {
NSLog(@"Geocoder failed with error: %@", error);
} else {
CLPlacemark *place = [placemarks objectAtIndex:0];
geocodedAddress = [NSString stringWithFormat:@"%@ %@, %@ %@", [place subThoroughfare], [place thoroughfare], [place locality], [place administrativeArea]];
if (UIGestureRecognizerStateBegan == [recognizer state]) {
addressPin = [[MapPoint alloc]initWithAddress:geocodedAddress coordinate:touchMapCoordinate
title:geocodedAddress];
[worldView addAnnotation:addressPin];
}
}
}];
}
そして、PinViewControllerがスタックにプッシュされるコードは次のとおりです。
-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
PinViewController *pinViewController = [[PinViewController alloc]init];
[[self navigationController]pushViewController:pinViewController animated:YES];
pinViewController.label.text = addressPin.title;
}
私が抱えている問題は、ラベルにジオコーディングされる最後の住所しか表示されていないことです。そのため、ピンをドロップしてコールアウトアクセサリボタンを押すと、正しいアドレスがPinViewControllerにプッシュされます。しかし、別の注釈のコールアウトアクセサリボタンを押すと、最後にドロップされたピンのアドレスがPinViewControllerにプッシュされます。したがって、注釈ビューのコールアウトアクセサリボタンを押したときに、注釈のタイトルがPinViewControllerに渡されるようにする方法を見つける必要があります。助けていただければ幸いです。