|
| 1 | +/* Copyright (c) 2014-present, Facebook, Inc. |
| 2 | + * All rights reserved. |
| 3 | + * |
| 4 | + * This source code is licensed under the BSD-style license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. An additional grant |
| 6 | + * of patent rights can be found in the PATENTS file in the same directory. |
| 7 | + */ |
| 8 | + |
| 9 | +#import "ASMapNode.h" |
| 10 | +#import <AsyncDisplayKit/ASInsetLayoutSpec.h> |
| 11 | +#import <AsyncDisplayKit/ASCenterLayoutSpec.h> |
| 12 | +#import <AsyncDisplayKit/ASThread.h> |
| 13 | + |
| 14 | +@interface ASMapNode() |
| 15 | +{ |
| 16 | + ASDN::RecursiveMutex _propertyLock; |
| 17 | + CGSize _nodeSize; |
| 18 | + MKMapSnapshotter *_snapshotter; |
| 19 | + MKMapSnapshotOptions *_options; |
| 20 | + CGSize _maxSize; |
| 21 | + NSArray *_annotations; |
| 22 | +} |
| 23 | +@end |
| 24 | + |
| 25 | +@implementation ASMapNode |
| 26 | + |
| 27 | +@synthesize hasLiveMap = _hasLiveMap; |
| 28 | +@synthesize mapSize = _mapSize; |
| 29 | +@synthesize automaticallyReloadsMapImageOnOrientationChange = _automaticallyReloadsMapImageOnOrientationChange; |
| 30 | +@synthesize mapDelegate = _mapDelegate; |
| 31 | + |
| 32 | +- (instancetype)initWithCoordinate:(CLLocationCoordinate2D)coordinate |
| 33 | +{ |
| 34 | + if (!(self = [super init])) { |
| 35 | + return nil; |
| 36 | + } |
| 37 | + self.backgroundColor = ASDisplayNodeDefaultPlaceholderColor(); |
| 38 | + _hasLiveMap = YES; |
| 39 | + _automaticallyReloadsMapImageOnOrientationChange = YES; |
| 40 | + _options = [[MKMapSnapshotOptions alloc] init]; |
| 41 | + _options.region = MKCoordinateRegionMakeWithDistance(coordinate, 1000, 1000);; |
| 42 | + |
| 43 | + _mapImage = [[ASImageNode alloc]init]; |
| 44 | + _mapImage.clipsToBounds = YES; |
| 45 | + [self addSubnode:_mapImage]; |
| 46 | + [self updateGesture]; |
| 47 | + _maxSize = self.bounds.size; |
| 48 | + return self; |
| 49 | +} |
| 50 | + |
| 51 | +- (void)addAnnotations:(NSArray *)annotations |
| 52 | +{ |
| 53 | + ASDN::MutexLocker l(_propertyLock); |
| 54 | + if (annotations.count == 0) { |
| 55 | + return; |
| 56 | + } |
| 57 | + _annotations = [annotations copy]; |
| 58 | + if (annotations.count != _annotations.count && _mapImage.image) { |
| 59 | + // Redraw |
| 60 | + [self setNeedsDisplay]; |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +- (void)setUpSnapshotter |
| 65 | +{ |
| 66 | + if (!_snapshotter) { |
| 67 | + _options.size = _nodeSize; |
| 68 | + _snapshotter = [[MKMapSnapshotter alloc] initWithOptions:_options]; |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +- (BOOL)hasLiveMap |
| 73 | +{ |
| 74 | + ASDN::MutexLocker l(_propertyLock); |
| 75 | + return _hasLiveMap; |
| 76 | +} |
| 77 | + |
| 78 | +- (void)setHasLiveMap:(BOOL)hasLiveMap |
| 79 | +{ |
| 80 | + ASDN::MutexLocker l(_propertyLock); |
| 81 | + if (hasLiveMap == _hasLiveMap) |
| 82 | + return; |
| 83 | + |
| 84 | + _hasLiveMap = hasLiveMap; |
| 85 | + [self updateGesture]; |
| 86 | +} |
| 87 | + |
| 88 | +- (CGSize)mapSize |
| 89 | +{ |
| 90 | + ASDN::MutexLocker l(_propertyLock); |
| 91 | + return _mapSize; |
| 92 | +} |
| 93 | + |
| 94 | +- (void)setMapSize:(CGSize)mapSize |
| 95 | +{ |
| 96 | + ASDN::MutexLocker l(_propertyLock); |
| 97 | + if (CGSizeEqualToSize(mapSize,_mapSize)) { |
| 98 | + return; |
| 99 | + } |
| 100 | + _mapSize = mapSize; |
| 101 | + _nodeSize = _mapSize; |
| 102 | + _automaticallyReloadsMapImageOnOrientationChange = NO; |
| 103 | + [self setNeedsLayout]; |
| 104 | +} |
| 105 | + |
| 106 | +- (BOOL)automaticallyReloadsMapImageOnOrientationChange |
| 107 | +{ |
| 108 | + ASDN::MutexLocker l(_propertyLock); |
| 109 | + return _automaticallyReloadsMapImageOnOrientationChange; |
| 110 | +} |
| 111 | + |
| 112 | +- (void)setAutomaticallyReloadsMapImageOnOrientationChange:(BOOL)automaticallyReloadsMapImageOnOrientationChange |
| 113 | +{ |
| 114 | + ASDN::MutexLocker l(_propertyLock); |
| 115 | + if (_automaticallyReloadsMapImageOnOrientationChange == automaticallyReloadsMapImageOnOrientationChange) { |
| 116 | + return; |
| 117 | + } |
| 118 | + _automaticallyReloadsMapImageOnOrientationChange = automaticallyReloadsMapImageOnOrientationChange; |
| 119 | + |
| 120 | +} |
| 121 | + |
| 122 | +- (void)updateGesture |
| 123 | +{ |
| 124 | + _hasLiveMap ? [self addTarget:self action:@selector(showLiveMap) forControlEvents:ASControlNodeEventTouchUpInside] : [self removeTarget:self action:@selector(showLiveMap) forControlEvents:ASControlNodeEventTouchUpInside]; |
| 125 | +} |
| 126 | + |
| 127 | +- (void)fetchData |
| 128 | +{ |
| 129 | + [super fetchData]; |
| 130 | + [self setUpSnapshotter]; |
| 131 | + [self takeSnapshot]; |
| 132 | +} |
| 133 | + |
| 134 | +- (void)clearFetchedData |
| 135 | +{ |
| 136 | + [super clearFetchedData]; |
| 137 | + if (_liveMap) { |
| 138 | + [_liveMap removeFromSupernode]; |
| 139 | + _liveMap = nil; |
| 140 | + } |
| 141 | + _mapImage.image = nil; |
| 142 | +} |
| 143 | + |
| 144 | +- (void)takeSnapshot |
| 145 | +{ |
| 146 | + if (!_snapshotter.isLoading) { |
| 147 | + [_snapshotter startWithCompletionHandler:^(MKMapSnapshot *snapshot, NSError *error) { |
| 148 | + if (!error) { |
| 149 | + UIImage *image = snapshot.image; |
| 150 | + CGRect finalImageRect = CGRectMake(0, 0, image.size.width, image.size.height); |
| 151 | + |
| 152 | + // Get a standard annotation view pin. Future implementations should use a custom annotation image property. |
| 153 | + MKAnnotationView *pin = [[MKPinAnnotationView alloc] initWithAnnotation:nil reuseIdentifier:@""]; |
| 154 | + UIImage *pinImage = pin.image; |
| 155 | + |
| 156 | + UIGraphicsBeginImageContextWithOptions(image.size, YES, image.scale); |
| 157 | + [image drawAtPoint:CGPointMake(0, 0)]; |
| 158 | + |
| 159 | + for (id<MKAnnotation>annotation in _annotations) |
| 160 | + { |
| 161 | + CGPoint point = [snapshot pointForCoordinate:annotation.coordinate]; |
| 162 | + if (CGRectContainsPoint(finalImageRect, point)) |
| 163 | + { |
| 164 | + CGPoint pinCenterOffset = pin.centerOffset; |
| 165 | + point.x -= pin.bounds.size.width / 2.0; |
| 166 | + point.y -= pin.bounds.size.height / 2.0; |
| 167 | + point.x += pinCenterOffset.x; |
| 168 | + point.y += pinCenterOffset.y; |
| 169 | + [pinImage drawAtPoint:point]; |
| 170 | + } |
| 171 | + } |
| 172 | + UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext(); |
| 173 | + UIGraphicsEndImageContext(); |
| 174 | + _mapImage.image = finalImage; |
| 175 | + } |
| 176 | + }]; |
| 177 | + } |
| 178 | +} |
| 179 | + |
| 180 | +- (void)resetSnapshotter |
| 181 | +{ |
| 182 | + if (!_snapshotter.isLoading) { |
| 183 | + _options.size = _nodeSize; |
| 184 | + _snapshotter = [[MKMapSnapshotter alloc] initWithOptions:_options]; |
| 185 | + } |
| 186 | +} |
| 187 | + |
| 188 | +#pragma mark - Action |
| 189 | +- (void)showLiveMap |
| 190 | +{ |
| 191 | + if (self.isNodeLoaded && !_liveMap) { |
| 192 | + _liveMap = [[ASDisplayNode alloc]initWithViewBlock:^UIView *{ |
| 193 | + MKMapView *mapView = [[MKMapView alloc]initWithFrame:CGRectMake(0.0f, 0.0f, self.calculatedSize.width, self.calculatedSize.height)]; |
| 194 | + mapView.delegate = _mapDelegate; |
| 195 | + [mapView setRegion:_options.region]; |
| 196 | + [mapView addAnnotations:_annotations]; |
| 197 | + return mapView; |
| 198 | + }]; |
| 199 | + [self addSubnode:_liveMap]; |
| 200 | + _mapImage.image = nil; |
| 201 | + } |
| 202 | +} |
| 203 | + |
| 204 | +#pragma mark - Layout |
| 205 | +- (CGSize)calculateSizeThatFits:(CGSize)constrainedSize |
| 206 | +{ |
| 207 | + _nodeSize = CGSizeEqualToSize(CGSizeZero, _mapSize) ? CGSizeMake(constrainedSize.width, _options.size.height) : _mapSize; |
| 208 | + if (_mapImage) { |
| 209 | + [_mapImage calculateSizeThatFits:_nodeSize]; |
| 210 | + } |
| 211 | + return _nodeSize; |
| 212 | +} |
| 213 | + |
| 214 | +// 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. |
| 215 | +- (void)layout |
| 216 | +{ |
| 217 | + [super layout]; |
| 218 | + if (_liveMap) { |
| 219 | + MKMapView *mapView = (MKMapView *)_liveMap.view; |
| 220 | + mapView.frame = CGRectMake(0.0f, 0.0f, self.calculatedSize.width, self.calculatedSize.height); |
| 221 | + } |
| 222 | + else { |
| 223 | + _mapImage.frame = CGRectMake(0.0f, 0.0f, self.calculatedSize.width, self.calculatedSize.height); |
| 224 | + if (!CGSizeEqualToSize(_maxSize, self.bounds.size)) { |
| 225 | + _mapImage.preferredFrameSize = self.bounds.size; |
| 226 | + _maxSize = self.bounds.size; |
| 227 | + if (_automaticallyReloadsMapImageOnOrientationChange && _mapImage.image) { |
| 228 | + [self resetSnapshotter]; |
| 229 | + [self takeSnapshot]; |
| 230 | + } |
| 231 | + } |
| 232 | + } |
| 233 | +} |
| 234 | + |
| 235 | +@end |
0 commit comments