39

I have a MKPointAnnotation:

let ann = MKPointAnnotation()
self.ann.coordinate = annLoc
self.ann.title = "Customize me"
self.ann.subtitle = "???"
self.mapView.addAnnotation(ann)

It looks like this:

enter image description here

How can I customize this callout view to create my own view instead of the predefined one?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Michael
  • 32,527
  • 49
  • 210
  • 370
  • Check the following answer, Anna's answer http://stackoverflow.com/questions/25631410/swift-different-images-for-annotation – casillas Jun 12 '15 at 01:26
  • 1
    @casillas No, that's customizing the annotation. He's not trying to customize the annotation view, but rather its callout. I think you need something more like http://stackoverflow.com/a/17772487/1271826 (it's Objective-C, but the idea is the same in Swift). – Rob Jun 12 '15 at 03:49
  • You can also implement your custom view as callout. This may be an example. https://github.com/NabinRai4017/CallOutExample – Nabin Rai Jun 28 '17 at 11:14
  • You can try this: https://github.com/okhanokbay/MapViewPlus – Okhan Okbay Mar 25 '18 at 21:19

3 Answers3

83

It should first be noted that the simplest changes to the callout are enabled by simply adjusting the properties of the system provided callout, but customizing the right and left accessories (via rightCalloutAccessoryView and leftCalloutAccessoryView). You can do that configuration in viewForAnnotation.

Since iOS 9, we have access to the detailCalloutAccessoryView which, replaces the subtitle of the callout with a potentially visually rich view, while still enjoying the automatic rendition of the callout bubble (using auto layout makes this easier).

For example, here is a callout that used a MKSnapshotter to supply the image for an image view in the detail callout accessory as demonstrated in WWDC 2015 video What's New in MapKit:

enter image description here

You can achieve this with something like:

class SnapshotAnnotationView: MKPinAnnotationView {
    override var annotation: MKAnnotation? { didSet { configureDetailView() } }

    override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
        super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
        configure()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        configure()
    }
}

private extension SnapshotAnnotationView {
    func configure() {
        canShowCallout = true
        configureDetailView()
    }

    func configureDetailView() {
        guard let annotation = annotation else { return }

        let rect = CGRect(origin: .zero, size: CGSize(width: 300, height: 200))

        let snapshotView = UIView()
        snapshotView.translatesAutoresizingMaskIntoConstraints = false

        let options = MKMapSnapshotter.Options()
        options.size = rect.size
        options.mapType = .satelliteFlyover
        options.camera = MKMapCamera(lookingAtCenter: annotation.coordinate, fromDistance: 250, pitch: 65, heading: 0)

        let snapshotter = MKMapSnapshotter(options: options)
        snapshotter.start { snapshot, error in
            guard let snapshot = snapshot, error == nil else {
                print(error ?? "Unknown error")
                return
            }

            let imageView = UIImageView(frame: rect)
            imageView.image = snapshot.image
            snapshotView.addSubview(imageView)
        }

        detailCalloutAccessoryView = snapshotView
        NSLayoutConstraint.activate([
            snapshotView.widthAnchor.constraint(equalToConstant: rect.width),
            snapshotView.heightAnchor.constraint(equalToConstant: rect.height)
        ])
    }
}

Of course, you would then register that annotation view with your map, and no mapView(_:viewFor:) would be needed at all:

mapView.register(SnapshotAnnotationView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier)

If you're looking for a more radical redesign of the callout or need to support iOS versions prior to 9, it takes more work. The process entails (a) disabling the default callout; and (b) adding your own view when the user taps on the existing annotation view (i.e. the visual pin on the map).

The complexity then comes in the design of the callout, where you have to draw everything you want visible. E.g. if you want to draw a bubble to yield the popover feel of the call out, you have to do that yourself. But with some familiarity with how to draw shapes, images, text, etc., you should be able to render a callout that achieves the desired UX:

custom callout

Just add the view as a subview of the annotation view itself, and adjust its constraints accordingly:

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
    let calloutView = ...
    calloutView.translatesAutoresizingMaskIntoConstraints = false
    calloutView.backgroundColor = UIColor.lightGray
    view.addSubview(calloutView)

    NSLayoutConstraint.activate([
        calloutView.bottomAnchor.constraint(equalTo: view.topAnchor, constant: 0),
        calloutView.widthAnchor.constraint(equalToConstant: 60),
        calloutView.heightAnchor.constraint(equalToConstant: 30),
        calloutView.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: view.calloutOffset.x)
    ])
}

See https://github.com/robertmryan/CustomMapViewAnnotationCalloutSwift for an example of creating your own callout view. This only adds two labels, but it illustrates the fact that you can draw the bubble any shape you want, use constraints to dictate the size of the callout, etc.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • How do I specify the size off the calloutview? How do I change the border radius and can I use Autolayout in the calloutview? – Michael Jun 13 '15 at 23:54
  • In my example, I went old-school and instead of auto-layout, I actually calculated the width of the text using `sizeWithAttributes` on the string and then added two times the corner-radius to each the width (for left and right) and the height (for top and bottom), and then to the height, then also added the size of the arrow at the bottom. – Rob Jun 14 '15 at 01:36
  • I did not see where you have used ``sizeWithAttributes``. Could you please make this clear then I can accept your answer. – Michael Jun 14 '15 at 16:11
  • Rob could you help me here? http://stackoverflow.com/questions/34116577/how-to-color-the-bubble-border-upon-the-pin-on-a-map-using-detailcalloutaccessor – biggreentree Dec 07 '15 at 00:16
  • How can i set UIVIew instead of UIImageview? – ios developer Jul 28 '16 at 11:26
  • I don't understand: Just use a `UIView` (and whatever subviews) you want, and define constraints accordingly. It sounds like you might want to post your own question, showing us what you tried, what happened, and what you expected. – Rob Jul 28 '16 at 15:19
  • Rob , w.r.t to https://github.com/robertmryan/CustomMapViewAnnotationCalloutSwift , how can i navigate to view controller on click of "Details Button" – Badrinath Dec 18 '16 at 18:40
  • In the future, if you have a question on a GitHub repo, just post an "issue" on that repo. I posted your separate email as a [issue #3](https://github.com/robertmryan/CustomMapViewAnnotationCalloutSwift/issues/3) and answered your question there. – Rob Dec 18 '16 at 20:56
  • In your callout i see title. But for my case i don't want to show default title. I just want my own view. is it feasible? – hardik hadwani May 16 '19 at 09:31
  • @hardikhadwani - Not in the `detailCalloutAccessoryView` technique (because that appears to only show the callout if there is a title, too). But in the cumbersome, manual approach outlined in my GitHub repo, you can do whatever you want... – Rob May 16 '19 at 16:13
  • @Rob Can you help me out here ? https://stackoverflow.com/questions/57834000/ios-mapkit-custom-callout-with-image-and-text-on-top-of-a-pin-programmatically-u – Ali_C Sep 07 '19 at 14:42
  • @Rob Could you help me out here ? https://stackoverflow.com/questions/58319549/how-to-prevent-overlays-on-the-map-from-disappearing-when-zoom-scale-changes-in – Ali_C Oct 10 '19 at 10:17
  • @Rob I tried your example, which displays nicely. But after I added a button on such a detail callout view, tapping on the button or the detail view just closes the callout view, instead of triggering the button method or the `mapView:annotationView:calloutAccessoryControlTapped:`. Any pointers? – CodeBrew Jul 01 '21 at 01:42
3

No need to make MKAnnotationView Custom class just create an empty view .xib and design .xib as your requirement. Write your business login in UIView swift class.

Add the view on

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {

...

}

method like annotationView?.detailCalloutAccessoryView = customView

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    let annotationIdentifier = "AnnotationIdentifier"
    var annotationView: MKAnnotationView?
    if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier) {
        annotationView = dequeuedAnnotationView
        annotationView?.annotation = annotation
    } else {
        annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
    }
    if let annotation = annotation as? HPAnnotation {
       annotationView?.canShowCallout = true
       let customView = Bundle.main.loadNibNamed("HPAnnotationView", owner: self, options: nil)?.first as! HPAnnotationView
       customView.labelName.text = annotation.annotationTitle
       annotationView?.detailCalloutAccessoryView = customView
    }
    return annotationView
 }

If you want dynamic value show on callout view then first make MKAnnotation custom class where you can pass objects as you need.

import MapKit
import AddressBook
import UIKit

class HPAnnotation: NSObject, MKAnnotation {

   let title: String?
   let annotationTitle: String
   init(title: String, annotationTitle: String = "") {
      self.title = title
      self.annotationTitle = annotationTitle
   }

   var subtitle: String? {
     return details
   }

}

and pass value when creating annotation

 for index in 0..<searchPeopleArray.count {
    let annotation = HPAnnotation(title: "", annotationTitle: "")
    mapView.addAnnotation(annotation)
}

N.B: Here HPAnnotationView is my custom view class and xib name. HPAnnotation is my custom MKAnnotation.

Samrat Pramanik
  • 201
  • 3
  • 8
  • this only replaces the detail callout element (one of three in the callout), not the entire callout. – Alnitak Nov 30 '22 at 20:35
2

Create Cocoa file with classtype MKAnnotationView

CustomeAnnotationView.h file

@interface CustomeAnnotationView : MKAnnotationView
@property (strong, nonatomic) UIButton *buttonCustomeCallOut;
- (void)setSelected:(BOOL)selected animated:(BOOL)animated;
@end

CustomeAnnotationView.m file

@implementation CustomeAnnotationView

-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated{

    [super setSelected:selected animated:animated];

    if(selected)
    {



            self.buttonCustomeCallOut = [UIButton buttonWithType:UIButtonTypeCustom];//iconShare//iconShareBlue

            [self.buttonCustomeCallOut addTarget:self action:@selector(buttonHandlerCallOut:) forControlEvents:UIControlEventTouchDown];
        [self.buttonCustomeCallOut setBackgroundColor:[UIColor blueColor]];

            [self.buttonCustomeCallOut setFrame:CGRectMake(-40,-80, 100, 100)];



            [self addSubview:self.buttonCustomeCallOut];

        [self.buttonCustomeCallOut setUserInteractionEnabled:YES];
    }
    else
    {
        //Remove your custom view...
        [self.buttonCustomeCallOut setUserInteractionEnabled:NO];
        [self.buttonCustomeCallOut removeFromSuperview];

        self.buttonCustomeCallOut=nil;
    }
}
-(void)buttonHandlerCallOut:(UIButton*)sender{
    NSLog(@"Annotation Clicked");
}

- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent*)event
{
    UIView* v = [super hitTest:point withEvent:event];
    if (v != nil)
    {
        [self.superview bringSubviewToFront:self];
    }
    return v;
}

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent*)event
{
    CGRect rec = self.bounds;
    BOOL isIn = CGRectContainsPoint(rec, point);
    if(!isIn)
    {
        for (UIView *v in self.subviews)
        {
            isIn = CGRectContainsPoint(v.frame, point);
            if(isIn)
                break;
        }
    }
    return isIn;
}
@end

place this code where u want to create custome call out

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
    static NSString *identifier = @"CustAnnotation";

        CustomeAnnotationView *annotationView = (CustomeAnnotationView *) [self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
        if (annotationView == nil) {
            annotationView = [[CustomeAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
        }

        annotationView.enabled = YES;
        annotationView.canShowCallout = NO;
        annotationView.centerOffset = CGPointMake(0,-10);//-18

        return annotationView;
}
M.Othman
  • 5,132
  • 3
  • 35
  • 39
Yagnesh Dobariya
  • 2,241
  • 19
  • 29