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

Commit

Permalink
[ios] Add annotation view initializer with annotation and reuse id (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
boundsj authored May 19, 2017
1 parent 7612e23 commit 554b1cf
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 8 deletions.
1 change: 1 addition & 0 deletions platform/ios/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Mapbox welcomes participation and contributions from everyone. Please read [CONT

### Annotations

* Added a new initializer to `MGLAnnotationView` so that it is possible to create a new instance with an associated annotation object. ([#9029](https://github.com/mapbox/mapbox-gl-native/pull/9029))
* Fixed an issue causing a view-backed annotation to disappear immediately instead of animating when the annotation’s `coordinate` property is set to a value outside the current viewport. ([#8565](https://github.com/mapbox/mapbox-gl-native/pull/8565))
* Fixed an issue in which `MGLMapView` overrode the tint colors of its annotation views. ([#8789](https://github.com/mapbox/mapbox-gl-native/pull/8789))

Expand Down
30 changes: 30 additions & 0 deletions platform/ios/src/MGLAnnotationView.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,36 @@ typedef NS_ENUM(NSUInteger, MGLAnnotationViewDragState) {
*/
- (instancetype)initWithReuseIdentifier:(nullable NSString *)reuseIdentifier;

/**
Initializes and returns a new annotation view object.
Providing an annotation allows you to explicitly associate the annotation instance
with the new view and, in custom subclasses of `MGLAnnotationView`, customize the view
based on properties of the annotation instance in an overridden initializer. However,
annotation views that are reused will not necessarily be associated with the
same annotation they were initialized with. Also, annotation views that are in
the reuse queue will have a nil value for the annotation property. Passing an annotation
instance to the view is optional and the map view will automatically associate annotations
with views when views are provided to the map via the `-[MGLMapViewDelegate mapView:viewForAnnotation:]`
method.
The reuse identifier provides a way for you to improve performance by recycling
annotation views as they enter and leave the map’s viewport. As an annotation
leaves the viewport, the map view moves its associated view to a reuse queue.
When a new annotation becomes visible, you can request a view for that
annotation by passing the appropriate reuse identifier string to the
`-[MGLMapView dequeueReusableAnnotationViewWithIdentifier:]` method.
@param annotation The annotation object to associate with the new view.
@param reuseIdentifier A unique string identifier for this view that allows you
to reuse this view with multiple similar annotations. You can set this
parameter to `nil` if you don’t intend to reuse the view, but it is a good
idea in general to specify a reuse identifier to avoid creating redundant
views.
@return The initialized annotation view object.
*/
- (instancetype)initWithAnnotation:(nullable id<MGLAnnotation>)annotation reuseIdentifier:(nullable NSString *)reuseIdentifier;

/**
Called when the view is removed from the reuse queue.
Expand Down
18 changes: 11 additions & 7 deletions platform/ios/src/MGLAnnotationView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,27 @@ @interface MGLAnnotationView () <UIGestureRecognizerDelegate>

@implementation MGLAnnotationView

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
self = [self initWithFrame:CGRectZero];
+ (BOOL)supportsSecureCoding {
return YES;
}

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier {
return [self initWithAnnotation:nil reuseIdentifier:reuseIdentifier];
}

- (instancetype)initWithAnnotation:(nullable id<MGLAnnotation>)annotation reuseIdentifier:(nullable NSString *)reuseIdentifier {
self = [super initWithFrame:CGRectZero];
if (self)
{
_lastAppliedScaleTransform = CATransform3DIdentity;
_annotation = annotation;
_reuseIdentifier = [reuseIdentifier copy];
_scalesWithViewingDistance = YES;
_enabled = YES;
}
return self;
}

+ (BOOL)supportsSecureCoding {
return YES;
}

- (instancetype)initWithCoder:(NSCoder *)decoder {
if (self = [super initWithCoder:decoder]) {
_reuseIdentifier = [decoder decodeObjectOfClass:[NSString class] forKey:@"reuseIdentifier"];
Expand Down
2 changes: 1 addition & 1 deletion platform/ios/test/MGLAnnotationViewTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ - (MGLAnnotationView *)mapView:(MGLMapView *)mapView viewForAnnotation:(id<MGLAn

if (!annotationView)
{
annotationView = [[MGLAnnotationView alloc] initWithReuseIdentifier:MGLTestAnnotationReuseIdentifer];
annotationView = [[MGLAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:MGLTestAnnotationReuseIdentifer];
}

_annotationView = annotationView;
Expand Down

0 comments on commit 554b1cf

Please sign in to comment.