views:

537

answers:

2

Is it possible to display a View controller with further details of the Map annotation on a new view controller which when popped returns back to the MKMap view with the annotations still on it at that position. I can't seem to find a way in the SDK documentation that seems to indicate that its possible.

+2  A: 

Found the answer to my own question if you do the following:

What you can do is use an observer instead so in the

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation

method you can add the code like this:

 //Add an observer for the selected-property on the MKAnnotationView. Delegate to self.
  [annotationView addObserver:self
            forKeyPath:@"selected"
               options:NSKeyValueObservingOptionNew
               context:GMAP_ANNOTATION_SELECTED];

  annotationView.annotation = annotation;
  annotationView.canShowCallout = NO;

then create an observer catch which will call the method to render whatever view when the user clicks the annotation on the screen:

- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary *)change
                       context:(void *)context{

  NSString *action = (NSString*)context;


  if([action isEqualToString:GMAP_ANNOTATION_SELECTED]){
    BOOL annotationAppeared = [[change valueForKey:@"new"] boolValue];
    if (annotationAppeared) {
      [self showAnnotation:((AssetAnnotationView*) object).annotation];
    }
    else {
      //NSLog(@"annotation deselected %@", ((AssetAnnotationView*) object).annotation.title);
      //[self hideAnnotation];
    }
  }
}

then have your method which displays a modal view or whatever you want:

- (void)showAnnotation:(AssetAnnotationView*)annotation {

  UINavigationController *aNavController = [[UINavigationController alloc] initWithRootViewController:self.assetInfoViewController];
    aNavController.navigationBar.barStyle = UIBarStyleBlack;

  [self presentModalViewController:aNavController animated:YES];
  [aNavController release];

}

Unselect in viewWillAppear:

NSArray *selected = [localMapView selectedAnnotations]; for(id annotation in selected) { [localMapView deselectAnnotation:annotation animated:NO]; }

vladzz
make sure you unselect the annotaiton in your viewDidAppear/viewWillAppear otherwise you will get into the same problem as me and not be able to select the annotation again.
vladzz
And how do you unselect the annotation? I'm trying here with no success.
goo
Not the most efficient way of doing this. But this method seems to work. NSArray *selected = [localMapView selectedAnnotations]; for(id<MKAnnotation> annotation in selected) { [localMapView deselectAnnotation:annotation animated:NO]; }
vladzz
A: 

You can also use a tag for every annotation you create. Works for me.

alecnash