From 424c12499cab69b935779d306d7d47d19139116e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Minh=20Nguy=E1=BB=85n?= Date: Wed, 11 Jan 2017 08:06:20 -0800 Subject: [PATCH] [ios, macos] Make MGLMapView.style property nullable (#7664) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * [ios, macos] Made MGLMapView.style property nullable MGLMapView’s style property is now nullable (optional in Swift). The property is set to nil while the style loads and in the event that the style has failed to load. * [ios, macos] Switch to delegate method * [macos] Create MGLMapView programmatically for layer tests When MGLMapView is created via a nib, -initWithCoder: is called, causing styleURL to be set to nil, in turn causing the default Streets style to be loaded, fooling MGLStyleLayerTests into thinking one-line has been loaded. Instead, create MGLMapView programmatically, passing the intended style URL into the initializer, preventing Streets from being loaded. --- platform/darwin/src/MGLCircleStyleLayer.h | 2 +- platform/darwin/src/MGLFillStyleLayer.h | 2 +- platform/darwin/src/MGLLineStyleLayer.h | 2 +- platform/darwin/src/MGLRasterSource.h | 2 +- platform/darwin/src/MGLRasterStyleLayer.h | 2 +- platform/darwin/src/MGLShapeSource.h | 2 +- platform/darwin/src/MGLSymbolStyleLayer.h | 2 +- platform/darwin/src/MGLVectorSource.h | 2 +- platform/darwin/src/MGLVectorStyleLayer.h | 2 +- .../test/MGLDocumentationExampleTests.swift | 48 +++++++++---------- platform/darwin/test/MGLStyleLayerTests.h | 1 - platform/darwin/test/MGLStyleLayerTests.m | 13 ++++- platform/darwin/test/MGLStyleLayerTests.xib | 29 +++-------- platform/darwin/test/MGLStyleTests.mm | 22 ++++++--- platform/ios/src/MGLMapView.h | 9 +++- platform/ios/src/MGLMapView.mm | 15 +++--- platform/macos/src/MGLMapView.h | 9 +++- platform/macos/src/MGLMapView.mm | 25 ++++------ 18 files changed, 99 insertions(+), 90 deletions(-) diff --git a/platform/darwin/src/MGLCircleStyleLayer.h b/platform/darwin/src/MGLCircleStyleLayer.h index 186dfd23f..1117077d5 100644 --- a/platform/darwin/src/MGLCircleStyleLayer.h +++ b/platform/darwin/src/MGLCircleStyleLayer.h @@ -72,7 +72,7 @@ typedef NS_ENUM(NSUInteger, MGLCircleTranslationAnchor) { ]) layer.circleOpacity = MGLStyleValue(rawValue: 0.7) layer.predicate = NSPredicate(format: "%K == %@", "marital-status", "married") - mapView.style.addLayer(layer) + mapView.style?.addLayer(layer) ``` */ @interface MGLCircleStyleLayer : MGLVectorStyleLayer diff --git a/platform/darwin/src/MGLFillStyleLayer.h b/platform/darwin/src/MGLFillStyleLayer.h index 6fae42cef..d05b21eb5 100644 --- a/platform/darwin/src/MGLFillStyleLayer.h +++ b/platform/darwin/src/MGLFillStyleLayer.h @@ -45,7 +45,7 @@ typedef NS_ENUM(NSUInteger, MGLFillTranslationAnchor) { layer.sourceLayerIdentifier = "parks" layer.fillColor = MGLStyleValue(rawValue: .green) layer.predicate = NSPredicate(format: "type == %@", "national-park") - mapView.style.addLayer(layer) + mapView.style?.addLayer(layer) ``` */ @interface MGLFillStyleLayer : MGLVectorStyleLayer diff --git a/platform/darwin/src/MGLLineStyleLayer.h b/platform/darwin/src/MGLLineStyleLayer.h index a9f1b4c86..9b37a9746 100644 --- a/platform/darwin/src/MGLLineStyleLayer.h +++ b/platform/darwin/src/MGLLineStyleLayer.h @@ -100,7 +100,7 @@ typedef NS_ENUM(NSUInteger, MGLLineTranslationAnchor) { layer.lineColor = MGLStyleValue(rawValue: .brown) layer.lineCap = MGLStyleValue(rawValue: NSValue(mglLineCap: .round)) layer.predicate = NSPredicate(format: "%K == %@", "trail-type", "mountain-biking") - mapView.style.addLayer(layer) + mapView.style?.addLayer(layer) ``` */ @interface MGLLineStyleLayer : MGLVectorStyleLayer diff --git a/platform/darwin/src/MGLRasterSource.h b/platform/darwin/src/MGLRasterSource.h index cd95e85c5..c2807c8d2 100644 --- a/platform/darwin/src/MGLRasterSource.h +++ b/platform/darwin/src/MGLRasterSource.h @@ -47,7 +47,7 @@ extern const MGLTileSourceOption MGLTileSourceOptionTileSize; MGLAttributionInfo(title: NSAttributedString(string: "© Mapbox"), url: URL(string: "http://mapbox.com")) ] ]) - mapView.style.addSource(source) + mapView.style?.addSource(source) ``` */ @interface MGLRasterSource : MGLTileSource diff --git a/platform/darwin/src/MGLRasterStyleLayer.h b/platform/darwin/src/MGLRasterStyleLayer.h index 54a3149e8..736e8ee2f 100644 --- a/platform/darwin/src/MGLRasterStyleLayer.h +++ b/platform/darwin/src/MGLRasterStyleLayer.h @@ -29,7 +29,7 @@ NS_ASSUME_NONNULL_BEGIN ```swift let layer = MGLRasterStyleLayer(identifier: "clouds", source: source) layer.rasterOpacity = MGLStyleValue(rawValue: 0.5) - mapView.style.addLayer(layer) + mapView.style?.addLayer(layer) ``` */ @interface MGLRasterStyleLayer : MGLForegroundStyleLayer diff --git a/platform/darwin/src/MGLShapeSource.h b/platform/darwin/src/MGLShapeSource.h index a276864be..8c8a2ab4e 100644 --- a/platform/darwin/src/MGLShapeSource.h +++ b/platform/darwin/src/MGLShapeSource.h @@ -105,7 +105,7 @@ extern const MGLShapeSourceOption MGLShapeSourceOptionSimplificationTolerance; let polyline = MGLPolylineFeature(coordinates: &coordinates, count: UInt(coordinates.count)) let shape = MGLShapeCollectionFeature(shapes: [polyline]) let source = MGLShapeSource(identifier: "lines", shape: shape, options: nil) - mapView.style.addSource(source) + mapView.style?.addSource(source) ``` */ @interface MGLShapeSource : MGLSource diff --git a/platform/darwin/src/MGLSymbolStyleLayer.h b/platform/darwin/src/MGLSymbolStyleLayer.h index 690624c85..c693978ab 100644 --- a/platform/darwin/src/MGLSymbolStyleLayer.h +++ b/platform/darwin/src/MGLSymbolStyleLayer.h @@ -273,7 +273,7 @@ typedef NS_ENUM(NSUInteger, MGLTextTranslationAnchor) { layer.textJustification = MGLStyleValue(rawValue: NSValue(mglTextJustification: .left)) layer.textAnchor = MGLStyleValue(rawValue: NSValue(mglTextAnchor: .left)) layer.predicate = NSPredicate(format: "%K == %@", "venue-type", "coffee") - mapView.style.addLayer(layer) + mapView.style?.addLayer(layer) ``` */ @interface MGLSymbolStyleLayer : MGLVectorStyleLayer diff --git a/platform/darwin/src/MGLVectorSource.h b/platform/darwin/src/MGLVectorSource.h index aa867adb6..5032ca386 100644 --- a/platform/darwin/src/MGLVectorSource.h +++ b/platform/darwin/src/MGLVectorSource.h @@ -35,7 +35,7 @@ NS_ASSUME_NONNULL_BEGIN MGLAttributionInfo(title: NSAttributedString(string: "© Mapbox"), url: URL(string: "http://mapbox.com")) ] ]) - mapView.style.addSource(source) + mapView.style?.addSource(source) ``` */ @interface MGLVectorSource : MGLTileSource diff --git a/platform/darwin/src/MGLVectorStyleLayer.h b/platform/darwin/src/MGLVectorStyleLayer.h index d33494c3e..360b86c51 100644 --- a/platform/darwin/src/MGLVectorStyleLayer.h +++ b/platform/darwin/src/MGLVectorStyleLayer.h @@ -144,7 +144,7 @@ NS_ASSUME_NONNULL_BEGIN let layer = MGLLineStyleLayer(identifier: "contour", source: terrain) layer.sourceLayerIdentifier = "contours" layer.predicate = NSPredicate(format: "(index == 5 || index == 10) && ele >= 1500.0") - mapView.style.addLayer(layer) + mapView.style?.addLayer(layer) ``` */ @property (nonatomic, nullable) NSPredicate *predicate; diff --git a/platform/darwin/test/MGLDocumentationExampleTests.swift b/platform/darwin/test/MGLDocumentationExampleTests.swift index 65c145456..5f0e83b2c 100644 --- a/platform/darwin/test/MGLDocumentationExampleTests.swift +++ b/platform/darwin/test/MGLDocumentationExampleTests.swift @@ -68,10 +68,10 @@ class MGLDocumentationExampleTests: XCTestCase, MGLMapViewDelegate { let polyline = MGLPolylineFeature(coordinates: &coordinates, count: UInt(coordinates.count)) let shape = MGLShapeCollectionFeature(shapes: [polyline]) let source = MGLShapeSource(identifier: "lines", shape: shape, options: nil) - mapView.style.addSource(source) + mapView.style?.addSource(source) //#-end-example-code - XCTAssertNotNil(mapView.style.source(withIdentifier: "lines")) + XCTAssertNotNil(mapView.style?.source(withIdentifier: "lines")) } func testMGLRasterSource() { @@ -84,10 +84,10 @@ class MGLDocumentationExampleTests: XCTestCase, MGLMapViewDelegate { MGLAttributionInfo(title: NSAttributedString(string: "© Mapbox"), url: URL(string: "http://mapbox.com")) ] ]) - mapView.style.addSource(source) + mapView.style?.addSource(source) //#-end-example-code - XCTAssertNotNil(mapView.style.source(withIdentifier: "clouds")) + XCTAssertNotNil(mapView.style?.source(withIdentifier: "clouds")) } func testMGLVectorSource() { @@ -99,15 +99,15 @@ class MGLDocumentationExampleTests: XCTestCase, MGLMapViewDelegate { MGLAttributionInfo(title: NSAttributedString(string: "© Mapbox"), url: URL(string: "http://mapbox.com")) ] ]) - mapView.style.addSource(source) + mapView.style?.addSource(source) //#-end-example-code - XCTAssertNotNil(mapView.style.source(withIdentifier: "pois")) + XCTAssertNotNil(mapView.style?.source(withIdentifier: "pois")) } func testMGLCircleStyleLayer() { let population = MGLVectorSource(identifier: "population", configurationURL: URL(string: "https://example.com/style.json")!) - mapView.style.addSource(population) + mapView.style?.addSource(population) //#-example-code let layer = MGLCircleStyleLayer(identifier: "circles", source: population) @@ -119,15 +119,15 @@ class MGLDocumentationExampleTests: XCTestCase, MGLMapViewDelegate { ]) layer.circleOpacity = MGLStyleValue(rawValue: 0.7) layer.predicate = NSPredicate(format: "%K == %@", "marital-status", "married") - mapView.style.addLayer(layer) + mapView.style?.addLayer(layer) //#-end-example-code - XCTAssertNotNil(mapView.style.layer(withIdentifier: "circles")) + XCTAssertNotNil(mapView.style?.layer(withIdentifier: "circles")) } func testMGLLineStyleLayer() { let trails = MGLVectorSource(identifier: "trails", configurationURL: URL(string: "https://example.com/style.json")!) - mapView.style.addSource(trails) + mapView.style?.addSource(trails) //#-example-code let layer = MGLLineStyleLayer(identifier: "trails-path", source: trails) @@ -139,30 +139,30 @@ class MGLDocumentationExampleTests: XCTestCase, MGLMapViewDelegate { layer.lineColor = MGLStyleValue(rawValue: .brown) layer.lineCap = MGLStyleValue(rawValue: NSValue(mglLineCap: .round)) layer.predicate = NSPredicate(format: "%K == %@", "trail-type", "mountain-biking") - mapView.style.addLayer(layer) + mapView.style?.addLayer(layer) //#-end-example-code - XCTAssertNotNil(mapView.style.layer(withIdentifier: "trails-path")) + XCTAssertNotNil(mapView.style?.layer(withIdentifier: "trails-path")) } func testMGLFillStyleLayer() { let parks = MGLVectorSource(identifier: "parks", configurationURL: URL(string: "https://example.com/style.json")!) - mapView.style.addSource(parks) + mapView.style?.addSource(parks) //#-example-code let layer = MGLFillStyleLayer(identifier: "parks", source: parks) layer.sourceLayerIdentifier = "parks" layer.fillColor = MGLStyleValue(rawValue: .green) layer.predicate = NSPredicate(format: "type == %@", "national-park") - mapView.style.addLayer(layer) + mapView.style?.addLayer(layer) //#-end-example-code - XCTAssertNotNil(mapView.style.layer(withIdentifier: "parks")) + XCTAssertNotNil(mapView.style?.layer(withIdentifier: "parks")) } func testMGLSymbolStyleLayer() { let pois = MGLVectorSource(identifier: "pois", configurationURL: URL(string: "https://example.com/style.json")!) - mapView.style.addSource(pois) + mapView.style?.addSource(pois) //#-example-code let layer = MGLSymbolStyleLayer(identifier: "coffeeshops", source: pois) @@ -179,10 +179,10 @@ class MGLDocumentationExampleTests: XCTestCase, MGLMapViewDelegate { layer.textJustification = MGLStyleValue(rawValue: NSValue(mglTextJustification: .left)) layer.textAnchor = MGLStyleValue(rawValue: NSValue(mglTextAnchor: .left)) layer.predicate = NSPredicate(format: "%K == %@", "venue-type", "coffee") - mapView.style.addLayer(layer) + mapView.style?.addLayer(layer) //#-end-example-code - XCTAssertNotNil(mapView.style.layer(withIdentifier: "coffeeshops")) + XCTAssertNotNil(mapView.style?.layer(withIdentifier: "coffeeshops")) } func testMGLRasterStyleLayer() { @@ -194,28 +194,28 @@ class MGLDocumentationExampleTests: XCTestCase, MGLMapViewDelegate { MGLAttributionInfo(title: NSAttributedString(string: "© Mapbox"), url: URL(string: "http://mapbox.com")) ] ]) - mapView.style.addSource(source) + mapView.style?.addSource(source) //#-example-code let layer = MGLRasterStyleLayer(identifier: "clouds", source: source) layer.rasterOpacity = MGLStyleValue(rawValue: 0.5) - mapView.style.addLayer(layer) + mapView.style?.addLayer(layer) //#-end-example-code - XCTAssertNotNil(mapView.style.layer(withIdentifier: "clouds")) + XCTAssertNotNil(mapView.style?.layer(withIdentifier: "clouds")) } func testMGLVectorStyleLayer$predicate() { let terrain = MGLVectorSource(identifier: "terrain", configurationURL: URL(string: "https://example.com/style.json")!) - mapView.style.addSource(terrain) + mapView.style?.addSource(terrain) //#-example-code let layer = MGLLineStyleLayer(identifier: "contour", source: terrain) layer.sourceLayerIdentifier = "contours" layer.predicate = NSPredicate(format: "(index == 5 || index == 10) && ele >= 1500.0") - mapView.style.addLayer(layer) + mapView.style?.addLayer(layer) //#-end-example-code - XCTAssertNotNil(mapView.style.layer(withIdentifier: "contour")) + XCTAssertNotNil(mapView.style?.layer(withIdentifier: "contour")) } } diff --git a/platform/darwin/test/MGLStyleLayerTests.h b/platform/darwin/test/MGLStyleLayerTests.h index 74ce62e89..65322ff33 100644 --- a/platform/darwin/test/MGLStyleLayerTests.h +++ b/platform/darwin/test/MGLStyleLayerTests.h @@ -5,7 +5,6 @@ @interface MGLStyleLayerTests : XCTestCase @property (nonatomic) IBOutlet MGLMapView *mapView; -@property (nonatomic) XCTestExpectation *expectation; @property (nonatomic, copy, readonly, class) NSString *layerType; - (void)testPropertyName:(NSString *)name isBoolean:(BOOL)isBoolean; diff --git a/platform/darwin/test/MGLStyleLayerTests.m b/platform/darwin/test/MGLStyleLayerTests.m index 66bdc0df0..e9ac0794a 100644 --- a/platform/darwin/test/MGLStyleLayerTests.m +++ b/platform/darwin/test/MGLStyleLayerTests.m @@ -11,18 +11,27 @@ - (void)setUp { [super setUp]; [MGLAccountManager setAccessToken:@"pk.feedcafedeadbeefbadebede"]; + NSURL *styleURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"one-liner" withExtension:@"json"]; #if TARGET_OS_IPHONE UIApplication *app = [UIApplication sharedApplication]; UIViewController *vc = [[UIViewController alloc] init]; app.keyWindow.rootViewController = vc; [vc view]; // Force load xib - _mapView = [[MGLMapView alloc] initWithFrame:CGRectMake(0, 0, 256, 256)]; + _mapView = [[MGLMapView alloc] initWithFrame:CGRectMake(0, 0, 256, 256) styleURL:styleURL]; [vc.view addSubview:_mapView]; - _mapView.delegate = self; #else NSWindowController *windowController = [[NSWindowController alloc] initWithWindowNibName:@"MGLStyleLayerTests" owner:self]; + NSView *contentView = windowController.window.contentView; + _mapView = [[MGLMapView alloc] initWithFrame:contentView.bounds styleURL:styleURL]; + [contentView addSubview:_mapView]; [windowController showWindow:nil]; #endif + _mapView.delegate = self; + XCTAssertNil(_mapView.style); + [self keyValueObservingExpectationForObject:self.mapView keyPath:@"style" handler:^BOOL(MGLMapView * _Nonnull observedMapView, NSDictionary * _Nonnull change) { + return observedMapView.style != nil; + }]; + [self waitForExpectationsWithTimeout:1 handler:nil]; } - (void)tearDown { diff --git a/platform/darwin/test/MGLStyleLayerTests.xib b/platform/darwin/test/MGLStyleLayerTests.xib index cc336191e..d21f5753f 100644 --- a/platform/darwin/test/MGLStyleLayerTests.xib +++ b/platform/darwin/test/MGLStyleLayerTests.xib @@ -1,38 +1,21 @@ - - + + - + + - - - - - + - + - - - - - - - - - - - - - - diff --git a/platform/darwin/test/MGLStyleTests.mm b/platform/darwin/test/MGLStyleTests.mm index bcad7ab50..b771ff0af 100644 --- a/platform/darwin/test/MGLStyleTests.mm +++ b/platform/darwin/test/MGLStyleTests.mm @@ -39,7 +39,13 @@ [super setUp]; [MGLAccountManager setAccessToken:@"pk.feedcafedeadbeefbadebede"]; - self.mapView = [[MGLMapView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; + NSURL *styleURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"one-liner" withExtension:@"json"]; + self.mapView = [[MGLMapView alloc] initWithFrame:CGRectMake(0, 0, 100, 100) styleURL:styleURL]; + XCTAssertNil(self.mapView.style); + [self keyValueObservingExpectationForObject:self.mapView keyPath:@"style" handler:^BOOL(MGLMapView * _Nonnull observedMapView, NSDictionary * _Nonnull change) { + return observedMapView.style != nil; + }]; + [self waitForExpectationsWithTimeout:1 handler:nil]; } - (void)tearDown { @@ -250,12 +256,16 @@ [self.style insertLayer:layer0 belowLayer:layer1]; NSArray *layers = [self.style layers]; + NSUInteger startIndex = 0; + if ([layers.firstObject.identifier isEqualToString:@"com.mapbox.annotations.points"]) { + startIndex++; + } - XCTAssert([[layers[0] identifier] isEqualToString:layer0.identifier]); - XCTAssert([[layers[1] identifier] isEqualToString:layer1.identifier]); - XCTAssert([[layers[2] identifier] isEqualToString:layer2.identifier]); - XCTAssert([[layers[3] identifier] isEqualToString:layer3.identifier]); - XCTAssert([[layers[4] identifier] isEqualToString:layer4.identifier]); + XCTAssertEqualObjects(layers[startIndex++].identifier, layer0.identifier); + XCTAssertEqualObjects(layers[startIndex++].identifier, layer1.identifier); + XCTAssertEqualObjects(layers[startIndex++].identifier, layer2.identifier); + XCTAssertEqualObjects(layers[startIndex++].identifier, layer3.identifier); + XCTAssertEqualObjects(layers[startIndex++].identifier, layer4.identifier); } @end diff --git a/platform/ios/src/MGLMapView.h b/platform/ios/src/MGLMapView.h index 771a48c7f..62f053e96 100644 --- a/platform/ios/src/MGLMapView.h +++ b/platform/ios/src/MGLMapView.h @@ -123,6 +123,13 @@ IB_DESIGNABLE Unlike the `styleURL` property, this property is set to an object that allows you to manipulate every aspect of the style locally. + If the style is loading, this property is set to `nil` until the style finishes + loading. If the style has failed to load, this property is set to `nil`. + Because the style loads asynchronously, you should manipulate it in the + `-[MGLMapViewDelegate mapView:didFinishLoadingStyle:]` or + `-[MGLMapViewDelegate mapViewDidFinishLoadingMap:]` method. It is not possible + to manipulate the style before it has finished loading. + @note The default styles provided by Mapbox contain sources and layers with identifiers that will change over time. Applications that use APIs that manipulate a style's sources and layers must first set the style URL to an @@ -130,7 +137,7 @@ IB_DESIGNABLE `+[MGLStyle outdoorsStyleURLWithVersion:]`, `MGLMapView`'s “Style URL” inspectable in Interface Builder, or a manually constructed `NSURL`. */ -@property (nonatomic, readonly) MGLStyle *style; +@property (nonatomic, readonly, nullable) MGLStyle *style; /** URLs of the styles bundled with the library. diff --git a/platform/ios/src/MGLMapView.mm b/platform/ios/src/MGLMapView.mm index ddb11ffbb..f8ca67309 100644 --- a/platform/ios/src/MGLMapView.mm +++ b/platform/ios/src/MGLMapView.mm @@ -347,6 +347,11 @@ public: return self; } ++ (NS_SET_OF(NSString *) *)keyPathsForValuesAffectingStyle +{ + return [NSSet setWithObject:@"styleURL"]; +} + + (NS_SET_OF(NSString *) *)keyPathsForValuesAffectingStyleURL { return [NSSet setWithObjects:@"styleURL__", nil]; @@ -369,10 +374,8 @@ public: } styleURL = styleURL.mgl_URLByStandardizingScheme; - [self willChangeValueForKey:@"style"]; - _style = [[MGLStyle alloc] initWithMapView:self]; + self.style = nil; _mbglMap->setStyleURL([[styleURL absoluteString] UTF8String]); - [self didChangeValueForKey:@"style"]; } - (IBAction)reloadStyle:(__unused id)sender { @@ -4682,11 +4685,7 @@ public: } case mbgl::MapChangeDidFinishLoadingStyle: { - [self.style willChangeValueForKey:@"name"]; - [self.style willChangeValueForKey:@"sources"]; - [self.style didChangeValueForKey:@"sources"]; - [self.style willChangeValueForKey:@"layers"]; - [self.style didChangeValueForKey:@"layers"]; + self.style = [[MGLStyle alloc] initWithMapView:self]; if ([self.delegate respondsToSelector:@selector(mapView:didFinishLoadingStyle:)]) { [self.delegate mapView:self didFinishLoadingStyle:self.style]; diff --git a/platform/macos/src/MGLMapView.h b/platform/macos/src/MGLMapView.h index ea87f3b33..954e5a2c7 100644 --- a/platform/macos/src/MGLMapView.h +++ b/platform/macos/src/MGLMapView.h @@ -94,6 +94,13 @@ IB_DESIGNABLE Unlike the `styleURL` property, this property is set to an object that allows you to manipulate every aspect of the style locally. + If the style is loading, this property is set to `nil` until the style finishes + loading. If the style has failed to load, this property is set to `nil`. + Because the style loads asynchronously, you should manipulate it in the + `-[MGLMapViewDelegate mapView:didFinishLoadingStyle:]` or + `-[MGLMapViewDelegate mapViewDidFinishLoadingMap:]` method. It is not possible + to manipulate the style before it has finished loading. + @note The default styles provided by Mapbox contain sources and layers with identifiers that will change over time. Applications that use APIs that manipulate a style's sources and layers must first set the style URL to an @@ -101,7 +108,7 @@ IB_DESIGNABLE `+[MGLStyle outdoorsStyleURLWithVersion:]`, `MGLMapView`'s “Style URL” inspectable in Interface Builder, or a manually constructed `NSURL`. */ -@property (nonatomic, readonly) MGLStyle *style; +@property (nonatomic, readonly, nullable) MGLStyle *style; /** URL of the style currently displayed in the receiver. diff --git a/platform/macos/src/MGLMapView.mm b/platform/macos/src/MGLMapView.mm index 5c555925c..9ed399c78 100644 --- a/platform/macos/src/MGLMapView.mm +++ b/platform/macos/src/MGLMapView.mm @@ -590,6 +590,10 @@ public: #pragma mark Style ++ (NS_SET_OF(NSString *) *)keyPathsForValuesAffectingStyle { + return [NSSet setWithObject:@"styleURL"]; +} + - (nonnull NSURL *)styleURL { NSString *styleURLString = @(_mbglMap->getStyleURL().c_str()).mgl_stringOrNilIfEmpty; return styleURLString ? [NSURL URLWithString:styleURLString] : [MGLStyle streetsStyleURLWithVersion:MGLStyleDefaultVersion]; @@ -611,10 +615,8 @@ public: } styleURL = styleURL.mgl_URLByStandardizingScheme; - [self willChangeValueForKey:@"style"]; - _style = [[MGLStyle alloc] initWithMapView:self]; + self.style = nil; _mbglMap->setStyleURL(styleURL.absoluteString.UTF8String); - [self didChangeValueForKey:@"style"]; } - (IBAction)reloadStyle:(__unused id)sender { @@ -623,6 +625,10 @@ public: self.styleURL = styleURL; } +- (mbgl::Map *)mbglMap { + return _mbglMap; +} + #pragma mark View hierarchy and drawing - (void)viewWillMoveToWindow:(NSWindow *)newWindow { @@ -916,11 +922,7 @@ public: } case mbgl::MapChangeDidFinishLoadingStyle: { - [self.style willChangeValueForKey:@"name"]; - [self.style willChangeValueForKey:@"sources"]; - [self.style didChangeValueForKey:@"sources"]; - [self.style willChangeValueForKey:@"layers"]; - [self.style didChangeValueForKey:@"layers"]; + self.style = [[MGLStyle alloc] initWithMapView:self]; if ([self.delegate respondsToSelector:@selector(mapView:didFinishLoadingStyle:)]) { [self.delegate mapView:self didFinishLoadingStyle:self.style]; @@ -2268,13 +2270,6 @@ public: } } -#pragma mark - Runtime styling - -- (mbgl::Map *)mbglMap -{ - return _mbglMap; -} - #pragma mark MGLMultiPointDelegate methods - (double)alphaForShapeAnnotation:(MGLShape *)annotation {