0

I have populated a couple of locations on MKMapView using MKPointAnnotation(s). On tapping annotations I am showing some options with UIActionSheet menu. The options have some delete functionality which would delete the selected annotation on map when user taps the delete option on UIActionSheet. The issue is that I am not able to determine which annotation point is clicked, I seem to have no reference to it.

The code that adds annotation point is:

while(looping array of locations)
{
MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init];
annotationPoint.coordinate = {coord of my location}
annotationPoint.title = [anObject objectForKey:@"castTitle"];
annotationPoint.subtitle = [anObject objectForKey:@"storeName"];

[self.mainMapView addAnnotation:annotationPoint];
}

The code to show UIActionSheet on tapping annotation is:

-(MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
    if([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
    MKPinAnnotationView *pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];

    pinView.animatesDrop = YES;
    pinView.canShowCallout = YES;
    pinView.pinColor = [self getAnnotationColor];

    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    [rightButton setTitle:annotation.title forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(showOptions:) forControlEvents:UIControlEventTouchUpInside];
    pinView.rightCalloutAccessoryView = rightButton;

    return pinView;
}

-(IBAction)showOptions:(id)sender
{
        UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(@"", @"") delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel", @"Cancel") destructiveButtonTitle:nil otherButtonTitles:NSLocalizedString(@"Delete", @"Delete"),      nil];

        [sheet showInView:[self.view window]];
}
Firdous
  • 4,624
  • 13
  • 41
  • 80

2 Answers2

3

You can also Create a class which inherits from MKPointAnnotation, and add an id property , and use your class :

@interface MyPointAnnotation : MKPointAnnotation 

@property int pointId;

@end

and then use it in your view controller:

create annotation:

    MyPointAnnotation *myAnnotation = [[MyPointAnnotation alloc]init];
    myAnnotation.coordinate= someCoordinates;
    [myAnnotation setTitle:@"i am annotation with id"];
    myAnnotation.pointId = 1;
    [self.mapView addAnnotation:myAnnotation];

if you have an array of coordinates you can loop and create annotations.

and you can even customize annotation view by its id:

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{

MKPinAnnotationView *view=(MKPinAnnotationView*)[self.mapView dequeueReusableAnnotationViewWithIdentifier:@"reuseme"];
  if (!view) {
      view=[[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"reuseme"];
  }


  if (((MyPointAnnotation *)annotation).pointId == 1)
  {
      //the annotation with id 1.
  }
return view;
}
Oleg Sh.
  • 49
  • 1
2

Seems like there are two approaches.

If only one annotation can be selected at a time, you could access the -selectedAnnotations property of the enclosing MKMapView.

Another approach is to inspect sender in showOptions:, which is a reference to the UIButton that triggered the action. Find out its enclosing MKAnnotationView, which will give you the associated -annotation. You could then either stash this as an ivar or (my preferred approach) use some runtime magic - in the form of objc_setAssociatedObject(), declared in <objc/runtime.h> - to attach a reference to the annotation to the action sheet, allowing easy retrieval in its delegate callback.

(You could actually do this back in the button creation phase if you wanted, and attach a reference to the annotation to the UIButton, which can be picked up directly in showOptions: and reattached to the action sheet.

But [MKMapView selectedAnnotations] I would think is the easier way to go if it suits your needs.

Conrad Shultz
  • 8,748
  • 2
  • 31
  • 33
  • I used selectedAnnotations. I can get the title and subtitle of the MKPointAnnotation, but inorder to accurately determine there should be an ID property like 'tag' in other controls, as titles and subtitles are not unique values, because I have to make changes to a backend array based on this selected annotation. – Firdous May 10 '12 at 09:18
  • just solved that by overriding MKPointAnnotation class and adding an ID property to it. thanks – Firdous May 10 '12 at 09:28