summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFredrik Karlsson <bjorn.fredrik.karlsson@gmail.com>2016-06-14 13:12:56 +0200
committerGitHub <noreply@github.com>2016-06-14 13:12:56 +0200
commitd564302c6d376da123f01028124b1702b13ad1ef (patch)
tree65931c0ceec1c9cd0a990c5b75963a08eaab4410
parenta557e1e2c9f76a9b01d2a973a9a162e14525a669 (diff)
downloadqtlocation-mapboxgl-d564302c6d376da123f01028124b1702b13ad1ef.tar.gz
[ios] fixes #5129 added annotation property to MGLAnnotationView (#5307)
Also added a test case for MGLAnnotationView
-rw-r--r--platform/ios/ios.xcodeproj/project.pbxproj4
-rw-r--r--platform/ios/src/MGLAnnotationView.h9
-rw-r--r--platform/ios/src/MGLAnnotationView.mm2
-rw-r--r--platform/ios/src/MGLAnnotationView_Private.h2
-rw-r--r--platform/ios/src/MGLMapView.mm4
-rw-r--r--platform/ios/test/MGLAnnotationViewTests.m62
6 files changed, 81 insertions, 2 deletions
diff --git a/platform/ios/ios.xcodeproj/project.pbxproj b/platform/ios/ios.xcodeproj/project.pbxproj
index cb1bd70199..ec0957a4f5 100644
--- a/platform/ios/ios.xcodeproj/project.pbxproj
+++ b/platform/ios/ios.xcodeproj/project.pbxproj
@@ -7,6 +7,7 @@
objects = {
/* Begin PBXBuildFile section */
+ 353D23961D0B0DFE002BE09D /* MGLAnnotationViewTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 353D23951D0B0DFE002BE09D /* MGLAnnotationViewTests.m */; };
4018B1C71CDC287F00F666AF /* MGLAnnotationView.mm in Sources */ = {isa = PBXBuildFile; fileRef = 4018B1C41CDC277F00F666AF /* MGLAnnotationView.mm */; };
4018B1C81CDC287F00F666AF /* MGLAnnotationView.mm in Sources */ = {isa = PBXBuildFile; fileRef = 4018B1C41CDC277F00F666AF /* MGLAnnotationView.mm */; };
4018B1C91CDC288A00F666AF /* MGLAnnotationView_Private.h in Headers */ = {isa = PBXBuildFile; fileRef = 4018B1C31CDC277F00F666AF /* MGLAnnotationView_Private.h */; };
@@ -325,6 +326,7 @@
/* End PBXCopyFilesBuildPhase section */
/* Begin PBXFileReference section */
+ 353D23951D0B0DFE002BE09D /* MGLAnnotationViewTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MGLAnnotationViewTests.m; sourceTree = "<group>"; };
4018B1C31CDC277F00F666AF /* MGLAnnotationView_Private.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MGLAnnotationView_Private.h; sourceTree = "<group>"; };
4018B1C41CDC277F00F666AF /* MGLAnnotationView.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = MGLAnnotationView.mm; sourceTree = "<group>"; };
4018B1C51CDC277F00F666AF /* MGLAnnotationView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MGLAnnotationView.h; sourceTree = "<group>"; };
@@ -653,6 +655,7 @@
DA2E885D1CC0382C00F24E7B /* MGLOfflinePackTests.m */,
DA2E885E1CC0382C00F24E7B /* MGLOfflineRegionTests.m */,
DA2E885F1CC0382C00F24E7B /* MGLOfflineStorageTests.m */,
+ 353D23951D0B0DFE002BE09D /* MGLAnnotationViewTests.m */,
DA2E88601CC0382C00F24E7B /* MGLStyleTests.mm */,
DA2E88551CC036F400F24E7B /* Info.plist */,
);
@@ -1351,6 +1354,7 @@
DA35A2C51CCA9F8300E826B2 /* MGLClockDirectionFormatterTests.m in Sources */,
DA2E88621CC0382C00F24E7B /* MGLOfflinePackTests.m in Sources */,
DA35A2AA1CCA058D00E826B2 /* MGLCoordinateFormatterTests.m in Sources */,
+ 353D23961D0B0DFE002BE09D /* MGLAnnotationViewTests.m in Sources */,
DA0CD5901CF56F6A00A5F5A5 /* MGLFeatureTests.mm in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
diff --git a/platform/ios/src/MGLAnnotationView.h b/platform/ios/src/MGLAnnotationView.h
index acf270dfcb..3b44432dc0 100644
--- a/platform/ios/src/MGLAnnotationView.h
+++ b/platform/ios/src/MGLAnnotationView.h
@@ -4,6 +4,8 @@
NS_ASSUME_NONNULL_BEGIN
+@protocol MGLAnnotation;
+
/** The MGLAnnotationView class is responsible for representing point-based annotation markers as a view. Annotation views represent an annotation object, which is an object that corresponds to the MGLAnnotation protocol. When an annotation’s coordinate point is visible on the map view, the map view delegate is asked to provide a corresponding annotation view. If an annotation view is created with a reuse identifier, the map view may recycle the view when it goes offscreen. */
@interface MGLAnnotationView : UIView
@@ -16,6 +18,13 @@ NS_ASSUME_NONNULL_BEGIN
- (instancetype)initWithReuseIdentifier:(nullable NSString *)reuseIdentifier;
/**
+ This property will be set to the associated annotation when the view is visible.
+
+ When the view is queued and waiting to be reused, the value will be set to nil.
+ */
+@property (nonatomic, readonly, nullable) id <MGLAnnotation> annotation;
+
+/**
The string that identifies that this annotation view is reusable. (read-only)
You specify the reuse identifier when you create the view. You use the identifier later to retrieve an annotation view that was
diff --git a/platform/ios/src/MGLAnnotationView.mm b/platform/ios/src/MGLAnnotationView.mm
index 1a95beb23d..04a888f5e6 100644
--- a/platform/ios/src/MGLAnnotationView.mm
+++ b/platform/ios/src/MGLAnnotationView.mm
@@ -8,8 +8,8 @@
@interface MGLAnnotationView ()
-@property (nonatomic) id<MGLAnnotation> annotation;
@property (nonatomic, readwrite, nullable) NSString *reuseIdentifier;
+@property (nonatomic, readwrite, nullable) id <MGLAnnotation> annotation;
@end
diff --git a/platform/ios/src/MGLAnnotationView_Private.h b/platform/ios/src/MGLAnnotationView_Private.h
index c5a65487a2..d7cdea194c 100644
--- a/platform/ios/src/MGLAnnotationView_Private.h
+++ b/platform/ios/src/MGLAnnotationView_Private.h
@@ -5,8 +5,8 @@ NS_ASSUME_NONNULL_BEGIN
@interface MGLAnnotationView (Private)
-@property (nonatomic) id<MGLAnnotation> annotation;
@property (nonatomic, readwrite, nullable) NSString *reuseIdentifier;
+@property (nonatomic, readwrite, nullable) id <MGLAnnotation> annotation;
- (void)setCenter:(CGPoint)center pitch:(CGFloat)pitch;
diff --git a/platform/ios/src/MGLMapView.mm b/platform/ios/src/MGLMapView.mm
index 90e0186429..a1a412af01 100644
--- a/platform/ios/src/MGLMapView.mm
+++ b/platform/ios/src/MGLMapView.mm
@@ -2866,6 +2866,7 @@ mbgl::Duration MGLDurationInSeconds(NSTimeInterval duration)
if (annotationView)
{
annotationViewsForAnnotation[annotationValue] = annotationView;
+ annotationView.annotation = annotation;
annotationView.center = [self convertCoordinate:annotation.coordinate toPointToView:self];
[newAnnotationViews addObject:annotationView];
}
@@ -3083,6 +3084,7 @@ mbgl::Duration MGLDurationInSeconds(NSTimeInterval duration)
MGLAnnotationContext &annotationContext = _annotationContextsByAnnotationTag.at(annotationTag);
MGLAnnotationView *annotationView = annotationContext.annotationView;
+ annotationView.annotation = nil;
[annotationView removeFromSuperview];
if (annotationTag == _selectedAnnotationTag)
@@ -4540,6 +4542,8 @@ mbgl::Duration MGLDurationInSeconds(NSTimeInterval duration)
if (!annotationView) return;
+ annotationView.annotation = nil;
+
if (annotationContext.viewReuseIdentifier)
{
NSMutableArray *annotationViewReuseQueue = [self annotationViewReuseQueueForIdentifier:annotationContext.viewReuseIdentifier];
diff --git a/platform/ios/test/MGLAnnotationViewTests.m b/platform/ios/test/MGLAnnotationViewTests.m
new file mode 100644
index 0000000000..541c43b5a1
--- /dev/null
+++ b/platform/ios/test/MGLAnnotationViewTests.m
@@ -0,0 +1,62 @@
+#import <Mapbox/Mapbox.h>
+#import <XCTest/XCTest.h>
+
+static NSString * const MGLTestAnnotationReuseIdentifer = @"MGLTestAnnotationReuseIdentifer";
+
+@interface MGLTestAnnotation : NSObject <MGLAnnotation>
+@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
+@end
+
+@implementation MGLTestAnnotation
+@end
+
+@interface MGLAnnotationViewTests : XCTestCase <MGLMapViewDelegate>
+@property (nonatomic) XCTestExpectation *expectation;
+@property (nonatomic) MGLMapView *mapView;
+@property (nonatomic, weak) MGLAnnotationView *annotationView;
+@end
+
+@implementation MGLAnnotationViewTests
+
+- (void)setUp
+{
+ [super setUp];
+ _mapView = [[MGLMapView alloc] initWithFrame:CGRectZero];
+ _mapView.delegate = self;
+}
+
+- (void)testAnnotationView
+{
+ _expectation = [self expectationWithDescription:@"annotation property"];
+
+ MGLTestAnnotation *annotation = [[MGLTestAnnotation alloc] init];
+ [_mapView addAnnotation:annotation];
+
+ [self waitForExpectationsWithTimeout:1 handler:nil];
+
+ XCTAssert(_mapView.annotations.count == 1, @"number of annotations should be 1");
+ XCTAssertNotNil(_annotationView.annotation, @"annotation property should not be nil");
+
+ [_mapView removeAnnotation:_annotationView.annotation];
+
+ XCTAssert(_mapView.annotations.count == 0, @"number of annotations should be 0");
+ XCTAssertNil(_annotationView.annotation, @"annotation property should be nil");
+}
+
+- (MGLAnnotationView *)mapView:(MGLMapView *)mapView viewForAnnotation:(id<MGLAnnotation>)annotation
+{
+ MGLAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:MGLTestAnnotationReuseIdentifer];
+
+ if (!annotationView)
+ {
+ annotationView = [[MGLAnnotationView alloc] initWithReuseIdentifier:MGLTestAnnotationReuseIdentifer];
+ }
+
+ _annotationView = annotationView;
+
+ [_expectation fulfill];
+
+ return annotationView;
+}
+
+@end