1

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;
}
user3599133
  • 155
  • 3
  • 17

2 Answers2

6

Selects the specified annotation and displays a callout view for it.

[mapView selectAnnotation:pinView animated:YES]; //here pinView is your annotation and mapview is your map

If the specified annotation is not onscreen, and therefore does not have an associated annotation view, this method has no effect.

click another place on map till my annotation deselect. its a default behavior of mapview.

MKMapView Reference

opening-callout-of-annotation-on-map-automatically-when-map-first-load

Community
  • 1
  • 1
Pawan Rai
  • 3,434
  • 4
  • 32
  • 42
0

The same solution with swift 3

 func mapView(_ mapView: MKMapView, didAdd views: [MKAnnotationView]) {
    self.mapView.selectAnnotation(self.mapView.annotations[0], animated: true)
}
user3126427
  • 855
  • 2
  • 14
  • 29