Check if MKAnnotation is selected on the map?

I have a very simple question: how can I check if MKAnotation is selected on the map?

I do not see the property of the selected type (GET).

I hope that the solution will not be triggered by the selected / canceled events and will save its result in the property and check them if necessary. There should be more simple.

Thank you very much!

+4
source share
3 answers

Check out -[MKMapView selectedAnnotations] .

+6
source

Using the delegate method of the MKMapView method didSelectAnnotationView didSelectAnnotationView: may receive the MKAnnotation Selected event

 -(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view { // Annotation is your custom class that holds information about the annotation if ([view.annotation isKindOfClass:[Annotation class]]) { Annotation *annot = view.annotation; NSInteger index = [self.arrayOfAnnotations indexOfObject:annot]; } } 

Hope this helps you.

+6
source

Just an update for this - in iOS 4 there are MKMapViewDelegate methods that you can use to track and MKMapViewDelegate annotations:

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

You can use the Observer event for the selected annotation:

 [pin addObserver:self forKeyPath:@"selected" options:NSKeyValueObservingOptionNew context:@"ANSELECTED"]; - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{ NSString *action = (NSString*)context; if([action isEqualToString:@"ANSELECTED"]){ BOOL annotationAppeared = [[change valueForKey:@"new"] boolValue]; if (annotationAppeared) { // clicked on an Annotation } else { // Annotation disselected } } } 
+1
source

Source: https://habr.com/ru/post/1479398/


All Articles