Skip to content
This repository was archived by the owner on Feb 2, 2023. It is now read-only.

Commit eb7caa3

Browse files
committed
Merge pull request #1036 from aaronschubert0/ASMapNode
[ASMapNode] now supports MKMapSnapshotOptions to specify map parameters
2 parents ccbf38c + e8f5f61 commit eb7caa3

2 files changed

Lines changed: 34 additions & 21 deletions

File tree

AsyncDisplayKit/ASMapNode.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ NS_ASSUME_NONNULL_BEGIN
1515
@interface ASMapNode : ASImageNode
1616

1717
/**
18-
The current region of ASMapNode. This can be set at any time and ASMapNode will animate the change. This property may be set from a background thread before the node is loaded, and will automatically be applied to define the region of the static snapshot (if .liveMap = NO) or the internal MKMapView (otherwise).
18+
The current options of ASMapNode. This can be set at any time and ASMapNode will animate the change.<br><br>This property may be set from a background thread before the node is loaded, and will automatically be applied to define the behavior of the static snapshot (if .liveMap = NO) or the internal MKMapView (otherwise).<br><br> Changes to the region and camera options will only be animated when when the liveMap mode is enabled, otherwise these options will be applied statically to the new snapshot. <br><br> The options object is used to specify properties even when the liveMap mode is enabled, allowing seamless transitions between the snapshot and liveMap (as well as back to the snapshot).
1919
*/
20-
@property (nonatomic, assign) MKCoordinateRegion region;
20+
@property (nonatomic, strong) MKMapSnapshotOptions *options;
2121

2222
/**
2323
This is the MKMapView that is the live map part of ASMapNode. This will be nil if .liveMap = NO. Note, MKMapView is *not* thread-safe.
@@ -41,7 +41,7 @@ NS_ASSUME_NONNULL_BEGIN
4141
@property (nonatomic, weak) id <MKMapViewDelegate> mapDelegate;
4242

4343
/**
44-
* @discussion This method set the annotations of the static map view and also to the live map view. Passing an empty array clears the map of any annotations.
44+
* @discussion This method sets the annotations of the static map view and also to the live map view. Passing an empty array clears the map of any annotations.
4545
* @param annotations An array of objects that conform to the MKAnnotation protocol
4646
*/
4747
- (void)setAnnotations:(NSArray<id<MKAnnotation>> *)annotations;

AsyncDisplayKit/ASMapNode.mm

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ @interface ASMapNode()
1818
{
1919
ASDN::RecursiveMutex _propertyLock;
2020
MKMapSnapshotter *_snapshotter;
21-
MKMapSnapshotOptions *_options;
2221
NSArray *_annotations;
2322
CLLocationCoordinate2D _centerCoordinateOfMap;
2423
}
@@ -28,7 +27,7 @@ @implementation ASMapNode
2827

2928
@synthesize needsMapReloadOnBoundsChange = _needsMapReloadOnBoundsChange;
3029
@synthesize mapDelegate = _mapDelegate;
31-
@synthesize region = _region;
30+
@synthesize options = _options;
3231
@synthesize liveMap = _liveMap;
3332

3433
#pragma mark - Lifecycle
@@ -43,13 +42,6 @@ - (instancetype)init
4342
_needsMapReloadOnBoundsChange = YES;
4443
_liveMap = NO;
4544
_centerCoordinateOfMap = kCLLocationCoordinate2DInvalid;
46-
47-
//Default world-scale view
48-
_region = MKCoordinateRegionForMapRect(MKMapRectWorld);
49-
50-
_options = [[MKMapSnapshotOptions alloc] init];
51-
_options.region = _region;
52-
5345
return self;
5446
}
5547

@@ -119,20 +111,19 @@ - (void)setNeedsMapReloadOnBoundsChange:(BOOL)needsMapReloadOnBoundsChange
119111
_needsMapReloadOnBoundsChange = needsMapReloadOnBoundsChange;
120112
}
121113

122-
- (MKCoordinateRegion)region
114+
- (MKMapSnapshotOptions *)options
123115
{
124116
ASDN::MutexLocker l(_propertyLock);
125-
return _region;
117+
return _options;
126118
}
127119

128-
- (void)setRegion:(MKCoordinateRegion)region
120+
- (void)setOptions:(MKMapSnapshotOptions *)options
129121
{
130122
ASDN::MutexLocker l(_propertyLock);
131-
_region = region;
123+
_options = options;
132124
if (self.isLiveMap) {
133-
[_mapView setRegion:_region animated:YES];
125+
[self applySnapshotOptions];
134126
} else {
135-
_options.region = _region;
136127
[self resetSnapshotter];
137128
[self takeSnapshot];
138129
}
@@ -183,7 +174,9 @@ - (void)takeSnapshot
183174
- (void)setUpSnapshotter
184175
{
185176
ASDisplayNodeAssert(!CGSizeEqualToSize(CGSizeZero, self.calculatedSize), @"self.calculatedSize can not be zero. Make sure that you are setting a preferredFrameSize or wrapping ASMapNode in a ASRatioLayoutSpec or similar.");
186-
_options.size = self.calculatedSize;
177+
if (!_options) {
178+
[self createInitialOptions];
179+
}
187180
_snapshotter = [[MKMapSnapshotter alloc] initWithOptions:_options];
188181
}
189182

@@ -193,6 +186,23 @@ - (void)resetSnapshotter
193186
_snapshotter = [[MKMapSnapshotter alloc] initWithOptions:_options];
194187
}
195188

189+
- (void)createInitialOptions
190+
{
191+
_options = [[MKMapSnapshotOptions alloc] init];
192+
//Default world-scale view
193+
_options.region = MKCoordinateRegionForMapRect(MKMapRectWorld);
194+
_options.size = self.calculatedSize;
195+
}
196+
197+
- (void)applySnapshotOptions
198+
{
199+
[_mapView setCamera:_options.camera animated:YES];
200+
[_mapView setRegion:_options.region animated:YES];
201+
[_mapView setMapType:_options.mapType];
202+
_mapView.showsBuildings = _options.showsBuildings;
203+
_mapView.showsPointsOfInterest = _options.showsPointsOfInterest;
204+
}
205+
196206
#pragma mark - Actions
197207
- (void)addLiveMap
198208
{
@@ -201,7 +211,10 @@ - (void)addLiveMap
201211
__weak ASMapNode *weakSelf = self;
202212
_mapView = [[MKMapView alloc] initWithFrame:CGRectZero];
203213
_mapView.delegate = weakSelf.mapDelegate;
204-
[_mapView setRegion:_options.region];
214+
if (!_options) {
215+
[weakSelf createInitialOptions];
216+
}
217+
[weakSelf applySnapshotOptions];
205218
[_mapView addAnnotations:_annotations];
206219
[weakSelf setNeedsLayout];
207220
[weakSelf.view addSubview:_mapView];
@@ -232,7 +245,7 @@ - (void)setAnnotations:(NSArray *)annotations
232245
}
233246

234247
#pragma mark - Layout
235-
// Layout isn't usually needed in the box model, but since we are making use of MKMapView which is hidden in an ASDisplayNode this is preferred.
248+
// Layout isn't usually needed in the box model, but since we are making use of MKMapView this is preferred.
236249
- (void)layout
237250
{
238251
[super layout];

0 commit comments

Comments
 (0)