diff options
author | Minh Nguyễn <mxn@1ec5.org> | 2019-04-28 17:23:23 -0700 |
---|---|---|
committer | Minh Nguyễn <mxn@1ec5.org> | 2019-05-07 22:04:48 -0700 |
commit | 910639168aaad8f06749c5be184ae50dbe9d566b (patch) | |
tree | 9bd1642efe2c9ed65b51a8a317602975b7c5c73f /platform/macos/app | |
parent | 84a11ba4e5a6c64edd63b81d606e2ae57862c798 (diff) | |
download | qtlocation-mapboxgl-upstream/1ec5-circle-2167.tar.gz |
[ios, macos] Added circle geometryupstream/1ec5-circle-2167
Added an MGLCircle class that generates a many-sided polygon under the hood.
Diffstat (limited to 'platform/macos/app')
-rw-r--r-- | platform/macos/app/Base.lproj/MainMenu.xib | 4 | ||||
-rw-r--r-- | platform/macos/app/MapDocument.m | 78 |
2 files changed, 54 insertions, 28 deletions
diff --git a/platform/macos/app/Base.lproj/MainMenu.xib b/platform/macos/app/Base.lproj/MainMenu.xib index 6f8f24ce99..a359570d8a 100644 --- a/platform/macos/app/Base.lproj/MainMenu.xib +++ b/platform/macos/app/Base.lproj/MainMenu.xib @@ -542,9 +542,9 @@ <action selector="dropManyPins:" target="-1" id="Rtv-8N-3Z8"/> </connections> </menuItem> - <menuItem title="Add Polygon and Polyline" keyEquivalent="l" id="DVr-vT-lpe"> + <menuItem title="Simulate Nightfall" keyEquivalent="l" id="DVr-vT-lpe"> <connections> - <action selector="drawPolygonAndPolyLineAnnotations:" target="-1" id="EhT-CB-gee"/> + <action selector="addNightAndLighthouse:" target="-1" id="EhT-CB-gee"/> </connections> </menuItem> <menuItem title="Add Animated Annotation" id="Etf-JN-Aoc"> diff --git a/platform/macos/app/MapDocument.m b/platform/macos/app/MapDocument.m index 213aa33107..7716dfeb05 100644 --- a/platform/macos/app/MapDocument.m +++ b/platform/macos/app/MapDocument.m @@ -99,7 +99,7 @@ NSArray<id <MGLAnnotation>> *MBXFlattenedShapes(NSArray<id <MGLAnnotation>> *sha BOOL _showsToolTipsOnDroppedPins; BOOL _randomizesCursorsOnDroppedPins; BOOL _isTouringWorld; - BOOL _isShowingPolygonAndPolylineAnnotations; + BOOL _isShowingNightAndLighthouse; BOOL _isShowingAnimatedAnnotation; MGLMapSnapshotter *_snapshotter; @@ -627,7 +627,7 @@ NSArray<id <MGLAnnotation>> *MBXFlattenedShapes(NSArray<id <MGLAnnotation>> *sha - (IBAction)removeAllAnnotations:(id)sender { [self.mapView removeAnnotations:self.mapView.annotations]; - _isShowingPolygonAndPolylineAnnotations = NO; + _isShowingNightAndLighthouse = NO; _isShowingAnimatedAnnotation = NO; } @@ -673,33 +673,55 @@ NSArray<id <MGLAnnotation>> *MBXFlattenedShapes(NSArray<id <MGLAnnotation>> *sha self.mapView.camera = self.mapView.camera; } -- (IBAction)drawPolygonAndPolyLineAnnotations:(id)sender { +- (IBAction)addNightAndLighthouse:(id)sender { - if (_isShowingPolygonAndPolylineAnnotations) { - [self removeAllAnnotations:sender]; + if (_isShowingNightAndLighthouse) { return; } - _isShowingPolygonAndPolylineAnnotations = YES; + _isShowingNightAndLighthouse = YES; - // Pacific Northwest triangle - CLLocationCoordinate2D triangleCoordinates[3] = { - CLLocationCoordinate2DMake(44, -122), - CLLocationCoordinate2DMake(46, -122), - CLLocationCoordinate2DMake(46, -121) + // Daylight map, produced by multiple spherical caps simulating night + // https://en.wikipedia.org/wiki/Twilight + CLLocationDistance nightRadius = M_PI_2 * 6378137.0; + CLLocationDistance nightCapRadii[] = { + nightRadius, // civil twilight + nightRadius * (1.0 - 18.0 / 180 * 2), // nautical twilight + nightRadius * (1.0 - 12.0 / 180 * 2), // astronomical twilight + nightRadius * (1.0 - 6.0 / 180 * 2), // night }; - MGLPolygon *triangle = [MGLPolygon polygonWithCoordinates:triangleCoordinates count:3]; - [self.mapView addAnnotation:triangle]; - - // West coast line - CLLocationCoordinate2D lineCoordinates[4] = { - CLLocationCoordinate2DMake(47.6025, -122.3327), - CLLocationCoordinate2DMake(45.5189, -122.6726), - CLLocationCoordinate2DMake(37.7790, -122.4177), - CLLocationCoordinate2DMake(34.0532, -118.2349) - }; - MGLPolyline *line = [MGLPolyline polylineWithCoordinates:lineCoordinates count:4]; - [self.mapView addAnnotation:line]; + NSMutableArray *nightCaps = [NSMutableArray array]; + for (size_t i = 0; i < sizeof(nightCapRadii) / sizeof(nightCapRadii[0]); i++) { + MGLCircle *cap = [MGLCircle circleWithCenterCoordinate:CLLocationCoordinate2DMake(23.5, 0) + radius:nightCapRadii[i]]; + cap.title = @"Night"; + [nightCaps addObject:cap]; + } + [self.mapView addAnnotations:nightCaps]; + + // Concentric circles around Boston Light + // https://en.wikipedia.org/wiki/Boston_Light + CLLocationCoordinate2D epicenter = CLLocationCoordinate2DMake(42.32792025, -70.8901050288306); + NSMutableArray *concentricCircles = [NSMutableArray array]; + for (NSUInteger magnitude = 0; magnitude < 8; magnitude++) { + CLLocationDistance radius = exp(magnitude); + MGLCircle *circle = [MGLCircle circleWithCenterCoordinate:epicenter radius:radius]; + [concentricCircles addObject:circle]; + } + [self.mapView addAnnotations:concentricCircles]; + [self.mapView showAnnotations:@[concentricCircles[5]] animated:NO]; + + __block NSUInteger stepCount = 0; + [NSTimer scheduledTimerWithTimeInterval:1 repeats:YES block:^(NSTimer * _Nonnull timer) { + // Pulse each circle for a minute. + [concentricCircles enumerateObjectsUsingBlock:^(MGLCircle * _Nonnull circle, NSUInteger magnitude, BOOL * _Nonnull stop) { + circle.radius = exp(magnitude) + exp(magnitude - 1) * sin(M_PI / 10 * stepCount); + }]; + + if (++stepCount >= 60) { + [timer invalidate]; + } + }]; } - (IBAction)drawAnimatedAnnotation:(id)sender { @@ -1249,8 +1271,8 @@ NSArray<id <MGLAnnotation>> *MBXFlattenedShapes(NSArray<id <MGLAnnotation>> *sha if (menuItem.action == @selector(dropManyPins:)) { return YES; } - if (menuItem.action == @selector(drawPolygonAndPolyLineAnnotations:)) { - return !_isShowingPolygonAndPolylineAnnotations; + if (menuItem.action == @selector(addNightAndLighthouse:)) { + return !_isShowingNightAndLighthouse; } if (menuItem.action == @selector(drawAnimatedAnnotation:)) { return !_isShowingAnimatedAnnotation; @@ -1442,7 +1464,11 @@ NSArray<id <MGLAnnotation>> *MBXFlattenedShapes(NSArray<id <MGLAnnotation>> *sha } - (CGFloat)mapView:(MGLMapView *)mapView alphaForShapeAnnotation:(MGLShape *)annotation { - return 0.8; + return [annotation isKindOfClass:[MGLCircle class]] ? 0.1 : 0.8; +} + +- (NSColor *)mapView:(MGLMapView *)mapView fillColorForShape:(MGLShape *)shape { + return shape.title ? [NSColor blackColor] : [NSColor whiteColor]; } #pragma mark - MGLComputedShapeSourceDataSource |