diff options
author | Jason Wray <jason@mapbox.com> | 2016-05-04 21:01:25 -0400 |
---|---|---|
committer | Jason Wray <jason@mapbox.com> | 2016-07-26 18:54:02 -0700 |
commit | deb681b3a00263b2c80ee8d34cc050c7a4110b63 (patch) | |
tree | 5a4fca2418f1bb7e9c31472b8119cfbfa176af86 | |
parent | 239c5fbb3823a0c19d3dd350d31df89f322b50ec (diff) | |
download | qtlocation-mapboxgl-deb681b3a00263b2c80ee8d34cc050c7a4110b63.tar.gz |
Custom callout views, but must conform to protocol
-rw-r--r-- | platform/darwin/src/MGLAnnotation.h | 3 | ||||
-rw-r--r-- | platform/darwin/src/MGLShape.h | 3 | ||||
-rw-r--r-- | platform/ios/app/MBXCustomCalloutView.m | 27 | ||||
-rw-r--r-- | platform/ios/app/MBXViewController.m | 18 | ||||
-rw-r--r-- | platform/ios/ios.xcodeproj/project.pbxproj | 2 | ||||
-rw-r--r-- | platform/ios/src/MGLCalloutView.h | 29 | ||||
-rw-r--r-- | platform/ios/src/MGLMapView.mm | 36 |
7 files changed, 55 insertions, 63 deletions
diff --git a/platform/darwin/src/MGLAnnotation.h b/platform/darwin/src/MGLAnnotation.h index f8e5c3ca0a..24ce72a55c 100644 --- a/platform/darwin/src/MGLAnnotation.h +++ b/platform/darwin/src/MGLAnnotation.h @@ -4,6 +4,7 @@ #ifdef TARGET_OS_IPHONE #import <UIKit/UIKit.h> +#import "MGLCalloutView.h" #endif #import "MGLTypes.h" @@ -57,7 +58,7 @@ NS_ASSUME_NONNULL_BEGIN @property (nonatomic) BOOL canShowCallout; -@property (nonatomic, nullable) UIView *callout; +@property (nonatomic, nullable) UIView <MGLCalloutView> *calloutView; #endif diff --git a/platform/darwin/src/MGLShape.h b/platform/darwin/src/MGLShape.h index 90d932e9ec..a61e79c260 100644 --- a/platform/darwin/src/MGLShape.h +++ b/platform/darwin/src/MGLShape.h @@ -2,6 +2,7 @@ #ifdef TARGET_OS_IPHONE #import <UIKit/UIKit.h> +#import "MGLCalloutView.h" #endif #import "MGLAnnotation.h" @@ -34,7 +35,7 @@ NS_ASSUME_NONNULL_BEGIN @property (nonatomic) BOOL canShowCallout; -@property (nonatomic, nullable) UIView *callout; +@property (nonatomic, nullable) UIView <MGLCalloutView> *calloutView; #endif diff --git a/platform/ios/app/MBXCustomCalloutView.m b/platform/ios/app/MBXCustomCalloutView.m index 9edc00f6e9..bea747c9fd 100644 --- a/platform/ios/app/MBXCustomCalloutView.m +++ b/platform/ios/app/MBXCustomCalloutView.m @@ -10,16 +10,10 @@ static CGFloat const tipWidth = 10.0; @end @implementation MBXCustomCalloutView { - id <MGLAnnotation> _representedObject; - UIView *_leftAccessoryView; - UIView *_rightAccessoryView; - __weak id <MGLCalloutViewDelegate> _delegate; + NSString *_title; } -@synthesize representedObject = _representedObject; -@synthesize leftAccessoryView = _leftAccessoryView; -@synthesize rightAccessoryView = _rightAccessoryView; -@synthesize delegate = _delegate; +@synthesize title = _title; - (instancetype)initWithFrame:(CGRect)frame { @@ -40,16 +34,11 @@ static CGFloat const tipWidth = 10.0; - (void)presentCalloutFromRect:(CGRect)rect inView:(UIView *)view constrainedToView:(UIView *)constrainedView animated:(BOOL)animated { - if ([self.delegate respondsToSelector:@selector(calloutViewWillAppear:)]) - { - [self.delegate performSelector:@selector(calloutViewWillAppear:) withObject:self]; - } - [view addSubview:self]; // prepare title label - if ([self.representedObject respondsToSelector:@selector(title)]) + if (_title) { - self.mainLabel.text = self.representedObject.title; + self.mainLabel.text = _title; [self.mainLabel sizeToFit]; } // prepare our frame @@ -59,17 +48,14 @@ static CGFloat const tipWidth = 10.0; CGFloat frameOriginY = rect.origin.y - frameHeight; self.frame = CGRectMake(frameOriginX, frameOriginY, frameWidth, frameHeight); - - if ([self.delegate respondsToSelector:@selector(calloutViewDidAppear:)]) - { - [self.delegate performSelector:@selector(calloutViewDidAppear:) withObject:self]; - } } - (void)dismissCalloutAnimated:(BOOL)animated { if (self.superview) + { [self removeFromSuperview]; + } } #pragma mark - internals @@ -101,5 +87,4 @@ static CGFloat const tipWidth = 10.0; CGPathRelease(tipPath); } - @end diff --git a/platform/ios/app/MBXViewController.m b/platform/ios/app/MBXViewController.m index 50b4b7cf7d..13c5438071 100644 --- a/platform/ios/app/MBXViewController.m +++ b/platform/ios/app/MBXViewController.m @@ -430,9 +430,11 @@ static NSString * const MBXViewControllerAnnotationViewReuseIdentifer = @"MBXVie [self.mapView removeAnnotations:self.mapView.annotations]; MBXCustomCalloutAnnotation *annotation = [[MBXCustomCalloutAnnotation alloc] init]; - annotation.coordinate = CLLocationCoordinate2DMake(48.8533940, 2.3775439); + annotation.coordinate = CLLocationCoordinate2DMake(38.904722, -77.016389);//CLLocationCoordinate2DMake(48.8533940, 2.3775439); annotation.title = @"Custom Callout"; - + annotation.canShowCallout = YES; + annotation.calloutView = [[MBXCustomCalloutView alloc] init]; + [self.mapView addAnnotation:annotation]; [self.mapView showAnnotations:@[annotation] animated:YES]; } @@ -700,9 +702,15 @@ static NSString * const MBXViewControllerAnnotationViewReuseIdentifer = @"MBXVie return image; } -- (BOOL)mapView:(__unused MGLMapView *)mapView annotationCanShowCallout:(__unused id <MGLAnnotation>)annotation -{ - return YES; +- (void)mapView:(MGLMapView *)mapView didDeselectAnnotation:(id<MGLAnnotation>)annotation { + NSString *title = [(MGLPointAnnotation *)annotation title]; + if ( ! title.length) + { + return; + } + NSString *lastTwoCharacters = [title substringFromIndex:title.length - 2]; + MGLAnnotationImage *annotationImage = [mapView dequeueReusableAnnotationImageWithIdentifier:lastTwoCharacters]; + annotationImage.image = annotationImage.image ? nil : [self imageWithText:lastTwoCharacters backgroundColor:[UIColor grayColor]]; } - (CGFloat)mapView:(__unused MGLMapView *)mapView alphaForShapeAnnotation:(MGLShape *)annotation diff --git a/platform/ios/ios.xcodeproj/project.pbxproj b/platform/ios/ios.xcodeproj/project.pbxproj index c94dfe4a36..c089823ab8 100644 --- a/platform/ios/ios.xcodeproj/project.pbxproj +++ b/platform/ios/ios.xcodeproj/project.pbxproj @@ -26,6 +26,7 @@ 40FDA76B1CCAAA6800442548 /* MBXAnnotationView.m in Sources */ = {isa = PBXBuildFile; fileRef = 40FDA76A1CCAAA6800442548 /* MBXAnnotationView.m */; }; 554180421D2E97DE00012372 /* OpenGLES.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 554180411D2E97DE00012372 /* OpenGLES.framework */; }; DA0CD5901CF56F6A00A5F5A5 /* MGLFeatureTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = DA0CD58F1CF56F6A00A5F5A5 /* MGLFeatureTests.mm */; }; + 9686D6051CDAC6A200B6FA7B /* MBXCustomCalloutView.m in Sources */ = {isa = PBXBuildFile; fileRef = DA1DC9671CB6C6B7006E619F /* MBXCustomCalloutView.m */; }; DA17BE301CC4BAC300402C41 /* MGLMapView_Internal.h in Headers */ = {isa = PBXBuildFile; fileRef = DA17BE2F1CC4BAC300402C41 /* MGLMapView_Internal.h */; }; DA17BE311CC4BDAA00402C41 /* MGLMapView_Internal.h in Headers */ = {isa = PBXBuildFile; fileRef = DA17BE2F1CC4BAC300402C41 /* MGLMapView_Internal.h */; }; DA1DC96B1CB6C6B7006E619F /* MBXOfflinePacksTableViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = DA1DC9691CB6C6B7006E619F /* MBXOfflinePacksTableViewController.m */; }; @@ -1345,6 +1346,7 @@ DA1DC96B1CB6C6B7006E619F /* MBXOfflinePacksTableViewController.m in Sources */, DA1DC99B1CB6E064006E619F /* MBXViewController.m in Sources */, 40FDA76B1CCAAA6800442548 /* MBXAnnotationView.m in Sources */, + 9686D6051CDAC6A200B6FA7B /* MBXCustomCalloutView.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/platform/ios/src/MGLCalloutView.h b/platform/ios/src/MGLCalloutView.h index 902b273f11..3801bbcec2 100644 --- a/platform/ios/src/MGLCalloutView.h +++ b/platform/ios/src/MGLCalloutView.h @@ -13,35 +13,32 @@ NS_ASSUME_NONNULL_BEGIN */ @protocol MGLCalloutView <NSObject> -@property (nonatomic, strong) NSString *title; -@property (nonatomic, strong) NSString *subtitle; - /** - A view that the user may tap to perform an action. This view is conventionally - positioned on the left side of the callout view. + Presents a callout view by adding it to `inView` and pointing at the given rect of `inView`’s bounds. Constrains the callout to the bounds of the given view. */ -@property (nonatomic, strong) UIView *leftAccessoryView; +- (void)presentCalloutFromRect:(CGRect)rect inView:(UIView *)view constrainedToView:(UIView *)constrainedView animated:(BOOL)animated; /** - A view that the user may tap to perform an action. This view is conventionally - positioned on the right side of the callout view. + Dismisses the callout view. */ -@property (nonatomic, strong) UIView *rightAccessoryView; +- (void)dismissCalloutAnimated:(BOOL)animated; -@property (nonatomic, strong) UIView *detailAccessoryView; +@optional + +@property (nonatomic, strong) NSString *title; +@property (nonatomic, strong) NSString *subtitle; /** - Presents a callout view by adding it to `view` and pointing at the given rect - of `view`’s bounds. Constrains the callout to the bounds of the given view. + A view that the user may tap to perform an action. This view is conventionally positioned on the left side of the callout view. */ -- (void)presentCalloutFromRect:(CGRect)rect inView:(UIView *)view constrainedToView:(UIView *)constrainedView animated:(BOOL)animated; +@property (nonatomic, strong) UIView *leftAccessoryView; /** - Dismisses the callout view. + A view that the user may tap to perform an action. This view is conventionally positioned on the right side of the callout view. */ -- (void)dismissCalloutAnimated:(BOOL)animated; +@property (nonatomic, strong) UIView *rightAccessoryView; -@optional +@property (nonatomic, strong) UIView *detailAccessoryView; @property (nonatomic, weak) id<MGLCalloutViewDelegate> delegate; diff --git a/platform/ios/src/MGLMapView.mm b/platform/ios/src/MGLMapView.mm index e5e0331274..e4361f678d 100644 --- a/platform/ios/src/MGLMapView.mm +++ b/platform/ios/src/MGLMapView.mm @@ -3462,17 +3462,28 @@ mbgl::Duration MGLDurationInSeconds(NSTimeInterval duration) // build the callout UIView <MGLCalloutView> *calloutView; -// if ([self.delegate respondsToSelector:@selector(mapView:calloutViewForAnnotation:)]) -// { -// calloutView = [self.delegate mapView:self calloutViewForAnnotation:annotation]; -// } + if ([annotation respondsToSelector:@selector(calloutView)] && annotation.calloutView) + { + calloutView = annotation.calloutView; + } if (!calloutView) { - calloutView = [self calloutViewForAnnotation:annotation]; + calloutView = [MGLCompactCalloutView calloutView]; } - self.calloutViewForSelectedAnnotation = calloutView; + calloutView.tintColor = self.tintColor; + if ([calloutView respondsToSelector:@selector(title)]) + { + calloutView.title = annotation.title; + } + + if ([calloutView respondsToSelector:@selector(subtitle)]) + { + calloutView.subtitle = annotation.subtitle; + } + + self.calloutViewForSelectedAnnotation = calloutView; // consult delegate for left and/or right accessory views // if ([self.delegate respondsToSelector:@selector(mapView:leftCalloutAccessoryViewForAnnotation:)]) @@ -3501,9 +3512,6 @@ mbgl::Duration MGLDurationInSeconds(NSTimeInterval duration) // } // } - // set annotation delegate to handle taps on the callout view - //calloutView.delegate = self; - // present popup [calloutView presentCalloutFromRect:positioningRect inView:self.glView @@ -3523,16 +3531,6 @@ mbgl::Duration MGLDurationInSeconds(NSTimeInterval duration) } } -- (UIView <MGLCalloutView> *)calloutViewForAnnotation:(id <MGLAnnotation>)annotation -{ - MGLCompactCalloutView *calloutView = [MGLCompactCalloutView calloutView]; - calloutView.title = annotation.title; - calloutView.subtitle = annotation.subtitle; - calloutView.tintColor = self.tintColor; - - return calloutView; -} - /// Returns the rectangle that represents the annotation image of the annotation /// with the given tag. This rectangle is fitted to the image’s alignment rect /// and is appropriate for positioning a popover. |