0

I have a number of annotations I put on a mapView. This works fine with the code below

float userLatitude = [[userDict objectForKey:@"lat"]floatValue];
float userLongitude = [[userDict objectForKey:@"long"]floatValue];
NSString *someString = [someArray objectAtIndex:i];
NSString *anotherString = [anotherArray objectAtIndex:i];

    CLLocationCoordinate2D loopCoord = {.latitude =  userLatitude, .longitude =  userLongitude};

    MapAnnotationViewController *addAnnotation = [[MapAnnotationViewController alloc] initWithCoordinate:loopCoord];
        self.localImage = [UIImage imageNamed:@"first.png"];
        [addAnnotation setTitle:someString];
        [addAnnotation setSubTitle:anotherString];
        [mainMapView addAnnotation:addAnnotation];

I essentially want to add a custom touch even that fires when a person touches each annotation on the map. The method needs to fire from that specific location on the map (not the standard didSelect) How do I tag or pass a param from each annotation to the delegate method:

-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view

How do I get code like this:

UITapGestureRecognizer *singleFingerTap = 
  [[UITapGestureRecognizer alloc] initWithTarget:self 
                                          action:@selector(handleSingleTap:)];
[self.view addGestureRecognizer:singleFingerTap];
[singleFingerTap release];

//The event handling method
- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer {
  CGPoint location = [recognizer locationInView:[recognizer.view superview]];

  //Do stuff here...
}

passed to didSelectAnnotationView per specific annotation?

Eric
  • 4,063
  • 2
  • 27
  • 49
  • Can't you just access the annotation using view.annotation in the didSelectAnnotationView? –  Aug 17 '12 at 13:27
  • yeah, but would you add the tap gesture recognizer to the annotationClass then? and how would you do that so it's specific to each annotations – Eric Aug 17 '12 at 13:30
  • Why do you need the gesture recognizer? The delegate method will get called without it. –  Aug 17 '12 at 13:34
  • That was more an example of what I want, a tap that sends location of tap on a view – Eric Aug 17 '12 at 13:40
  • you can set a tag to every annotationview you added to map and when gesture happened you can get sender's tag easily to know which annotation touched. – iremk Aug 17 '12 at 15:20

1 Answers1

1

I believe the traditional way is to create a class that implements the MKAnnotation protocol and store your custom data in there. You'll need to store title, subtitle and coordinate as standard and you could add touchSelector. Then when the didSelect method gets called one of the parameters is the MKAnnotationView which has a property called annotation. Cast it to your custom class and then access that selector property.

-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
 {
   MyAnnotationClass *myAnno = (MyAnnotationClass *)view.annotation;
   //Do something with myAnno.touchSelector;
}

Here's a SO question about storing data in your own MKAnnotaion compliant class: Store data in MKAnnotation?

Community
  • 1
  • 1
Craig
  • 8,093
  • 8
  • 42
  • 74