How to create Custom MKAnnotationView and custom annotation title and subtitle
Asked Answered
T

1

19

enter image description here

I need to create above Annotation view on MKMapView. I am able to create the custom annotation view but on the tap of annotation the view need to be opened image with that big text, I am not able to create that one. Please provide me some links or the way to do this task.

Teofilateosinte answered 27/4, 2013 at 13:35 Comment(0)
B
38

To create a custom annotation view (your replacement for the standard pin), you can just set the image property of the MKAnnotationView in the viewForAnnotation method:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    if ([annotation isKindOfClass:[MKUserLocation class]])
    {
        return nil;
    }
    else if ([annotation isKindOfClass:[YourAnnotationClassHere class]]) // use whatever annotation class you used when creating the annotation
    {
        static NSString * const identifier = @"MyCustomAnnotation";

        MKAnnotationView* annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:identifier];

        if (annotationView)
        {
            annotationView.annotation = annotation;
        }
        else
        {
            annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation
                                                          reuseIdentifier:identifier];
        }

        annotationView.canShowCallout = NO; // set to YES if using customized rendition of standard callout; set to NO if creating your own callout from scratch
        annotationView.image = [UIImage imageNamed:@"your-image-here.png"];

        return annotationView;
    }
    return nil;
}

You might also want to adjust the centerOffset property to get the pin to line up precisely the way you want.

Regarding the customization of the callout, the easiest approach is to specify leftCalloutAccessoryView, rightCalloutAccessoryView and/or detailCalloutAccessoryView. This gives you a surprising degree of control, adding all sorts of images, labels, etc.

If you want to do a radical redesign of the callout, you can have viewForAnnotation set canShowCallout to NO and then respond to setSelected in your custom annotation view to show your own callout. While in Swift, see Customize MKAnnotation Callout View? for a few options for customizing the callouts.

Beestings answered 27/4, 2013 at 14:52 Comment(3)
@Teofilateosinte There is no standard popover control built into iOS for iPhone-sized devices. You'd have to use a third party library. If you google or search S.O. for "iphone popover", you'll see plenty of options.Beestings
This is presuming that you wanted to transition to another view controller, passing along the details of the annotation that you just selected. So, to do that, that destination would need some public property that specifies which annotation you selected. All that line of code does is set that public property in that destination view controller. This is how you pass information between view controllers.Beestings
@Rob- Thanks, I followed this approached and all works fine except that when a custom annotation is added, didSelectAnnotationView gets called automatically. Check my code here: #33297559 . Where am I going wrong?Hardhack

© 2022 - 2024 — McMap. All rights reserved.