Reputation: 2776
In my app, I have a mapView that drops a pin with a UILongPressGestureRecognizer. When the screen is pressed, the coordinate is geocoded and a pin is dropped, and the address is set as the title. There will be multiple pins on the screen, and the annotation view has a callout accessory that pushes another controller, called the PinViewController onto the stack. The PinViewController has a label, which I want to show the title of the pin.
Here is the code for dropping the pin:
-(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];
}
}
}];
}
And here is the code where the PinViewController is pushed to the stack:
-(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;
}
The trouble I am having is that the label is only showing the last address to be geocoded. So when I drop the a pin and press the callout accessory button, the correct address is pushed to the PinViewController. But if I press the callout accessory button of another annotation, the address of the last pin dropped is pushed to the PinViewController. So I need to find a way so that when I press the callout accessory button of a annotation view, the title of the annotation is passed to the PinViewController. I would really appreciate some help.
Upvotes: 0
Views: 402
Reputation: 17659
Your issue is that you are using addressPin
which is always going to be the last pin that was geocoded. You need to use the annotationView
to access the annotation that was pressed.
-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
PinViewController *pinViewController = [[PinViewController alloc]init];
[[self navigationController]pushViewController:pinViewController animated:YES];
pinViewController.label.text = view.annotation.title;
}
Upvotes: 2