Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

[ios] fixes #5129 added annotation property to MGLAnnotationView #5307

Merged
merged 1 commit into from
Jun 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions platform/ios/ios.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -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>"; };
Expand Down Expand Up @@ -653,6 +655,7 @@
DA2E885D1CC0382C00F24E7B /* MGLOfflinePackTests.m */,
DA2E885E1CC0382C00F24E7B /* MGLOfflineRegionTests.m */,
DA2E885F1CC0382C00F24E7B /* MGLOfflineStorageTests.m */,
353D23951D0B0DFE002BE09D /* MGLAnnotationViewTests.m */,
DA2E88601CC0382C00F24E7B /* MGLStyleTests.mm */,
DA2E88551CC036F400F24E7B /* Info.plist */,
);
Expand Down Expand Up @@ -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;
Expand Down
9 changes: 9 additions & 0 deletions platform/ios/src/MGLAnnotationView.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -15,6 +17,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)

Expand Down
2 changes: 1 addition & 1 deletion platform/ios/src/MGLAnnotationView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion platform/ios/src/MGLAnnotationView_Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
4 changes: 4 additions & 0 deletions platform/ios/src/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -2866,6 +2866,7 @@ - (void)addAnnotations:(NS_ARRAY_OF(id <MGLAnnotation>) *)annotations
if (annotationView)
{
annotationViewsForAnnotation[annotationValue] = annotationView;
annotationView.annotation = annotation;
annotationView.center = [self convertCoordinate:annotation.coordinate toPointToView:self];
[newAnnotationViews addObject:annotationView];
}
Expand Down Expand Up @@ -3083,6 +3084,7 @@ - (void)removeAnnotations:(NS_ARRAY_OF(id <MGLAnnotation>) *)annotations

MGLAnnotationContext &annotationContext = _annotationContextsByAnnotationTag.at(annotationTag);
MGLAnnotationView *annotationView = annotationContext.annotationView;
annotationView.annotation = nil;
[annotationView removeFromSuperview];

if (annotationTag == _selectedAnnotationTag)
Expand Down Expand Up @@ -4514,6 +4516,8 @@ - (void)enqueueAnnotationViewForAnnotationContext:(MGLAnnotationContext &)annota

if (!annotationView) return;

annotationView.annotation = nil;

if (annotationContext.viewReuseIdentifier)
{
NSMutableArray *annotationViewReuseQueue = [self annotationViewReuseQueueForIdentifier:annotationContext.viewReuseIdentifier];
Expand Down
62 changes: 62 additions & 0 deletions platform/ios/test/MGLAnnotationViewTests.m
Original file line number Diff line number Diff line change
@@ -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