iosuiimagemkpinannotationview

Add thumbnail to an MKPinAnnotation


I have this Custom MKPinAnnotation to which I need to add an image(Like a thumbnail). I should also be able to open the image fullscreen by tapping it. What is the best way to go around doing this?


Solution

  • A couple of thoughts.

    1. If you don't want a pin on the map, but rather some custom image, you can set your map's delegate and then write a viewForAnnotation that does something like:

      - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
      {
          if ([annotation isKindOfClass:[CustomAnnotation class]])
          {
              static NSString * const identifier = @"MyCustomAnnotation";
      
              // if you need to access your custom properties to your custom annotation, create a reference of the appropriate type:
      
              CustomAnnotation *customAnnotation = annotation;
      
              // try to dequeue an annotationView
      
              MKAnnotationView* annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
      
              if (annotationView)
              {
                  // if we found one, update its annotation appropriately
      
                  annotationView.annotation = annotation;
              }
              else
              {
                  // otherwise, let's create one
      
                  annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation
                                                                reuseIdentifier:identifier];
      
                  annotationView.image = [UIImage imageNamed:@"myimage"];
      
                  // if you want a callout with a "disclosure" button on it
      
                  annotationView.canShowCallout = YES;
                  annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
      
                  // If you want, if you're using QuartzCore.framework, you can add
                  // visual flourishes to your annotation view:
                  //
                  // [annotationView.layer setShadowColor:[UIColor blackColor].CGColor];
                  // [annotationView.layer setShadowOpacity:1.0f];
                  // [annotationView.layer setShadowRadius:5.0f];
                  // [annotationView.layer setShadowOffset:CGSizeMake(0, 0)];
                  // [annotationView setBackgroundColor:[UIColor whiteColor]];
              }
      
              return annotationView;
          }
      
          return nil;
      }
      
    2. If you do it with the standard callout (as shown above), you can then tell the map what you want to do when the user taps on the callout's disclosure button:

      - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
      {
          if (![view.annotation isKindOfClass:[CustomAnnotation class]])
              return;
      
          // do whatever you want to do to go to your next view
      }
      
    3. If you really want to bypass the callout with its disclosure button, but rather to go directly to another view controller when you tap on the annotation view, you would:

    For more information, see Annotating Maps in the Location Awareness Programming Guide.