Skip to content
This repository has been archived by the owner on Jun 14, 2024. It is now read-only.

Commit

Permalink
Custom callout: move annotation check to annotationCanShowCallout
Browse files Browse the repository at this point in the history
  • Loading branch information
friedbunny committed Aug 16, 2017
1 parent 398c888 commit 12e326b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 21 deletions.
18 changes: 6 additions & 12 deletions Examples/ObjectiveC/CustomCalloutViewExample.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,16 @@ - (void)viewDidLoad {
}

- (BOOL)mapView:(MGLMapView *)mapView annotationCanShowCallout:(id <MGLAnnotation>)annotation {
// Always allow callouts to popup when annotations are tapped.
return YES;
// Only show callouts for `Hello world!` annotation.
return [annotation respondsToSelector:@selector(title)] && [annotation.title isEqualToString:@"Hello world!"];
}

- (UIView<MGLCalloutView> *)mapView:(__unused MGLMapView *)mapView calloutViewForAnnotation:(id<MGLAnnotation>)annotation
{
// Only show callouts for `Hello world!` annotation.
if ([annotation respondsToSelector:@selector(title)]
&& [annotation.title isEqualToString:@"Hello world!"])
{
// Instantiate and return our custom callout view.
CustomCalloutView *calloutView = [[CustomCalloutView alloc] init];
calloutView.representedObject = annotation;
return calloutView;
}
return nil;
// Instantiate and return our custom callout view.
CustomCalloutView *calloutView = [[CustomCalloutView alloc] init];
calloutView.representedObject = annotation;
return calloutView;
}

- (void)mapView:(MGLMapView *)mapView tapOnCalloutForAnnotation:(id<MGLAnnotation>)annotation
Expand Down
13 changes: 4 additions & 9 deletions Examples/Swift/CustomCalloutViewExample.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,13 @@ class CustomCalloutViewExample_Swift: UIViewController, MGLMapViewDelegate {
}

func mapView(_ mapView: MGLMapView, annotationCanShowCallout annotation: MGLAnnotation) -> Bool {
// Always allow callouts to popup when annotations are tapped.
return true
// Only show callouts for `Hello world!` annotation.
return annotation.responds(to: #selector(getter: MGLAnnotation.title)) && annotation.title! == "Hello world!"
}

func mapView(_ mapView: MGLMapView, calloutViewFor annotation: MGLAnnotation) -> MGLCalloutView? {
// Only show callouts for `Hello world!` annotation.
if annotation.responds(to: #selector(getter: MGLAnnotation.title)) && annotation.title! == "Hello world!" {
// Instantiate and return our custom callout view.
return CustomCalloutView(representedObject: annotation)
}

return nil
// Instantiate and return our custom callout view.
return CustomCalloutView(representedObject: annotation)
}

func mapView(_ mapView: MGLMapView, tapOnCalloutFor annotation: MGLAnnotation) {
Expand Down

0 comments on commit 12e326b

Please sign in to comment.