[ASMapNode] Toggle user interaction when liveMap changes (#1753)

[ASMapNode] Change map snapshot when updating it with a new region (#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
This commit is contained in:
George A
2016-06-21 16:13:55 +02:00
parent 21d0f1e10d
commit 83d610cd54
15 changed files with 1108 additions and 9 deletions

View File

@@ -24,7 +24,7 @@
MKMapSnapshotter *_snapshotter;
BOOL _snapshotAfterLayout;
NSArray *_annotations;
CLLocationCoordinate2D _centerCoordinateOfMap;
// CLLocationCoordinate2D _centerCoordinateOfMap;
}
@end
@@ -46,8 +46,9 @@
_needsMapReloadOnBoundsChange = YES;
_liveMap = NO;
_centerCoordinateOfMap = kCLLocationCoordinate2DInvalid;
// _centerCoordinateOfMap = kCLLocationCoordinate2DInvalid;
_annotations = @[];
_showAnnotationsOptions = ASMapNodeShowAnnotationsOptionsIgnored;
return self;
}
@@ -55,7 +56,6 @@
{
[super didLoad];
if (self.isLiveMap) {
self.userInteractionEnabled = YES;
[self addLiveMap];
}
}
@@ -161,7 +161,18 @@
- (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
@@ -257,6 +268,7 @@
- (void)addLiveMap
{
ASDisplayNodeAssertMainThread();
self.userInteractionEnabled = YES;
if (!_mapView) {
__weak ASMapNode *weakSelf = self;
_mapView = [[MKMapView alloc] initWithFrame:CGRectZero];
@@ -265,17 +277,23 @@
[_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;
}
@@ -295,11 +313,44 @@
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
{