solución limpia para saber qué MKAnnotation, ha sido escogido?
-
22-09-2019 - |
Pregunta
Ok, por lo que suelen tener algún objeto X que desea ser anotada dentro de un MKMapView. Lo hace de esta manera:
DDAnnotation *annotation = [[DDAnnotation alloc] initWithCoordinate: poi.geoLocation.coordinate title: @"My Annotation"];
[_mapView addAnnotation: annotation];
A continuación, se crea la vista de anotación en el interior
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation;
Y cuando alguna llamada se dio un golpecito, usted maneja el interior de sucesos:
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control;
¿Cuál es la solución más limpia para pasar X hasta el último evento del grifo?
Solución
Si yo estoy entendiendo su pregunta, usted debe agregar una referencia o propiedad a su clase DDAnnotation de modo que en el método de calloutAccessoryControlTapped se puede acceder al objeto.
@interface DDAnnotation : NSObject <MKAnnotation> {
CLLocationCoordinate2D coordinate;
id objectX;
}
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic, retain) id objectX;
Cuando se crea la anotación:
DDAnnotation *annotation = [[DDAnnotation alloc] initWithCoordinate:poi.geoLocation.coordinate title: @"My Annotation"];
annotation.objectX = objectX;
[_mapView addAnnotation: annotation];
A continuación:
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control{
DDAnnotation *anno = view.annotation;
//access object via
[anno.objectX callSomeMethod];
}
Otros consejos
Lo hice y funcionó bien!
Es exactamente lo que necesito porque necesitaba hacer algo cuando el mapa se dio un golpecito, pero dejando que el grifo en la anotación fluya normalmente.
- (void)viewDidLoad
{
[super viewDidLoad];
UIGestureRecognizer *g = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)] autorelease];
g.cancelsTouchesInView = NO;
[self.mapView addGestureRecognizer:g];
}
- (void) handleGesture:(UIGestureRecognizer*)g{
if( g.state == UIGestureRecognizerStateEnded ){
NSSet *visibleAnnotations = [self.mapView annotationsInMapRect:self.mapView.visibleMapRect];
for ( id<MKAnnotation> annotation in visibleAnnotations.allObjects ){
UIView *av = [self.mapView viewForAnnotation:annotation];
CGPoint point = [g locationInView:av];
if( [av pointInside:point withEvent:nil] ){
// do what you wanna do when Annotation View has been tapped!
return;
}
}
//do what you wanna do when map is tapped
}
}