I have one map that I can to add pin (annotation) in it with code. I want when run my app and when add annotation in my app this annotation auto selected and when click on map instead annotation my annotation deselect. I can handling select and deselect my annotation but certainly must click on annotation till select and click on map (another place instead annotation) till my annotation deselect.
I don't want this. I want when run my app my annotation auto selected and only I need to click another place on map till my annotation deselect.
please guide me about it. this is my code for handling select/deselect annotation:
static NSString* const ANNOTATION_SELECTED_DESELECTED = @"mapAnnotationSelectedOrDeselected";
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
NSLog(@"2");
NSString *action = (__bridge NSString *)context;
if ([action isEqualToString:ANNOTATION_SELECTED_DESELECTED]) {
BOOL annotationSelected = [[change valueForKey:@"new"] boolValue];
if (annotationSelected) {
NSLog(@"Annotation was selected, do whatever required");
}else {
NSLog(@"Annotation was deselected, do what you must");
}
}
}
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views {
if ([mapViews.annotations count] > 1) {
for (MKAnnotationView *anAnnotationView in views) {
[anAnnotationView addObserver:self forKeyPath:@"selected" options:NSKeyValueObservingOptionNew context:(__bridge void *)(ANNOTATION_SELECTED_DESELECTED)];
}
}
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
MKPinAnnotationView *pinView = (MKPinAnnotationView*)[self.mapView dequeueReusableAnnotationViewWithIdentifier:@"Prospects"];
if ([annotation isKindOfClass:[MKUserLocation class]]){
return nil; //return nil to use default blue dot view
}
else if(pinView == nil) {
pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"Prospects"];
pinView.pinColor = MKPinAnnotationColorPurple;
pinView.animatesDrop = YES;
pinView.draggable = NO;
}
return pinView;
}