Skip to content

Commit

Permalink
[ASMapNode] Toggle user interaction when liveMap changes (facebookarc…
Browse files Browse the repository at this point in the history
…hive#1753)

[ASMapNode] Change map snapshot when updating it with a new region (facebookarchive#1754)
[ASMapNode] Commented out code that is causing inaccurate behavior
[ASMapNode] Added the ability to zoom in and show annotations, similar to showAnnotations:animated: of MKMapView.
Added a basic example for ASMapNode to try out the different changes
  • Loading branch information
george-gw committed Jun 21, 2016
1 parent 21d0f1e commit 83d610c
Show file tree
Hide file tree
Showing 15 changed files with 1,108 additions and 9 deletions.
16 changes: 16 additions & 0 deletions AsyncDisplayKit/ASMapNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@

NS_ASSUME_NONNULL_BEGIN

typedef NS_OPTIONS(NSUInteger, ASMapNodeShowAnnotationsOptions)
{
/** The annotations' positions are ignored, use the region or options specified instead. */
ASMapNodeShowAnnotationsOptionsIgnored = 0,
/** The annotations' positions are used to calculate the region to show in the map, equivalent to showAnnotations:animated. */
ASMapNodeShowAnnotationsOptionsZoomed = 1 << 0,
/** This will only have an effect if combined with the Zoomed state with liveMap turned on.*/
ASMapNodeShowAnnotationsOptionsAnimated = 1 << 1
};

@interface ASMapNode : ASImageNode

/**
Expand Down Expand Up @@ -54,6 +64,12 @@ NS_ASSUME_NONNULL_BEGIN
*/
@property (nonatomic, copy) NSArray<id<MKAnnotation>> *annotations;

/**
* @abstract This property specifies how to show the annotations.
* @default Default value is ASMapNodeShowAnnotationsIgnored
*/
@property (nonatomic, assign) ASMapNodeShowAnnotationsOptions showAnnotationsOptions;

@end

NS_ASSUME_NONNULL_END
Expand Down
69 changes: 60 additions & 9 deletions AsyncDisplayKit/ASMapNode.mm
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ @interface ASMapNode()
MKMapSnapshotter *_snapshotter;
BOOL _snapshotAfterLayout;
NSArray *_annotations;
CLLocationCoordinate2D _centerCoordinateOfMap;
// CLLocationCoordinate2D _centerCoordinateOfMap;
}
@end

Expand All @@ -46,16 +46,16 @@ - (instancetype)init

_needsMapReloadOnBoundsChange = YES;
_liveMap = NO;
_centerCoordinateOfMap = kCLLocationCoordinate2DInvalid;
// _centerCoordinateOfMap = kCLLocationCoordinate2DInvalid;
_annotations = @[];
_showAnnotationsOptions = ASMapNodeShowAnnotationsOptionsIgnored;
return self;
}

- (void)didLoad
{
[super didLoad];
if (self.isLiveMap) {
self.userInteractionEnabled = YES;
[self addLiveMap];
}
}
Expand Down Expand Up @@ -161,7 +161,18 @@ - (MKCoordinateRegion)region

- (void)setRegion:(MKCoordinateRegion)region
{
self.options.region = region;
MKMapSnapshotOptions * __weak oldOptions = self.options;
MKMapSnapshotOptions * options = [[MKMapSnapshotOptions alloc] init];
options.camera = oldOptions.camera;
options.mapRect = oldOptions.mapRect;
options.mapType = oldOptions.mapType;
options.showsPointsOfInterest = oldOptions.showsPointsOfInterest;
options.showsBuildings = oldOptions.showsBuildings;
options.size = oldOptions.size;
options.scale = oldOptions.scale;
options.region = region;
self.options = options;
// self.options.region = region;
}

#pragma mark - Snapshotter
Expand Down Expand Up @@ -257,6 +268,7 @@ - (void)applySnapshotOptions
- (void)addLiveMap
{
ASDisplayNodeAssertMainThread();
self.userInteractionEnabled = YES;
if (!_mapView) {
__weak ASMapNode *weakSelf = self;
_mapView = [[MKMapView alloc] initWithFrame:CGRectZero];
Expand All @@ -265,17 +277,23 @@ - (void)addLiveMap
[_mapView addAnnotations:_annotations];
[weakSelf setNeedsLayout];
[weakSelf.view addSubview:_mapView];

if (CLLocationCoordinate2DIsValid(_centerCoordinateOfMap)) {
[_mapView setCenterCoordinate:_centerCoordinateOfMap];

if (self.showAnnotationsOptions & ASMapNodeShowAnnotationsOptionsZoomed) {
BOOL const animated = self.showAnnotationsOptions & ASMapNodeShowAnnotationsOptionsAnimated;
[_mapView showAnnotations:_mapView.annotations animated:animated];
}

// if (CLLocationCoordinate2DIsValid(_centerCoordinateOfMap)) {
// [_mapView setCenterCoordinate:_centerCoordinateOfMap];
// }
}
}

- (void)removeLiveMap
{
self.userInteractionEnabled = false;
// FIXME: With MKCoordinateRegion, isn't the center coordinate fully specified? Do we need this?
_centerCoordinateOfMap = _mapView.centerCoordinate;
// _centerCoordinateOfMap = _mapView.centerCoordinate;
[_mapView removeFromSuperview];
_mapView = nil;
}
Expand All @@ -295,11 +313,44 @@ - (void)setAnnotations:(NSArray *)annotations
if (self.isLiveMap) {
[_mapView removeAnnotations:_mapView.annotations];
[_mapView addAnnotations:annotations];

if (self.showAnnotationsOptions & ASMapNodeShowAnnotationsOptionsZoomed) {
BOOL const animated = self.showAnnotationsOptions & ASMapNodeShowAnnotationsOptionsAnimated;
[_mapView showAnnotations:_mapView.annotations animated:animated];
}
} else {
[self takeSnapshot];
if (self.showAnnotationsOptions & ASMapNodeShowAnnotationsOptionsZoomed) {
self.region = [self regionToFitAnnotations:annotations];
}
else {
[self takeSnapshot];
}
}
}

-(MKCoordinateRegion)regionToFitAnnotations:(NSArray<id<MKAnnotation>> *)annotations
{
if([annotations count] == 0)
return MKCoordinateRegionForMapRect(MKMapRectWorld);

CLLocationCoordinate2D topLeftCoord = CLLocationCoordinate2DMake(-90, 180);
CLLocationCoordinate2D bottomRightCoord = CLLocationCoordinate2DMake(90, -180);

for (id<MKAnnotation> annotation in _annotations) {
topLeftCoord = CLLocationCoordinate2DMake(fmax(topLeftCoord.latitude, annotation.coordinate.latitude),
fmin(topLeftCoord.longitude, annotation.coordinate.longitude));
bottomRightCoord = CLLocationCoordinate2DMake(fmin(bottomRightCoord.latitude, annotation.coordinate.latitude),
fmax(bottomRightCoord.longitude, annotation.coordinate.longitude));
}

MKCoordinateRegion region = MKCoordinateRegionMake(CLLocationCoordinate2DMake(topLeftCoord.latitude - (topLeftCoord.latitude - bottomRightCoord.latitude) * 0.5,
topLeftCoord.longitude + (bottomRightCoord.longitude - topLeftCoord.longitude) * 0.5),
MKCoordinateSpanMake(fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * 2,
fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 2));

return region;
}

#pragma mark - Layout
- (void)setSnapshotSizeWithReloadIfNeeded:(CGSize)snapshotSize
{
Expand Down
6 changes: 6 additions & 0 deletions examples/ASMapNode/Podfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '7.0'
target 'Sample' do
pod 'AsyncDisplayKit', :path => '../..'
end

Loading

0 comments on commit 83d610c

Please sign in to comment.