diff --git a/platform/darwin/src/MGLStyle.h b/platform/darwin/src/MGLStyle.h index 1e0b87a73..cc87d7def 100644 --- a/platform/darwin/src/MGLStyle.h +++ b/platform/darwin/src/MGLStyle.h @@ -237,7 +237,7 @@ static const NSInteger MGLStyleDefaultVersion = 9; #pragma mark Managing Style Layers /** - The layers included in the style, arranged according to their front-to-back + The layers included in the style, arranged according to their back-to-front ordering on the screen. */ @property (nonatomic, strong) NS_MUTABLE_ARRAY_OF(MGLStyleLayer *) *layers; diff --git a/platform/darwin/src/MGLStyle.mm b/platform/darwin/src/MGLStyle.mm index c2ae75ed9..10f4aabe1 100644 --- a/platform/darwin/src/MGLStyle.mm +++ b/platform/darwin/src/MGLStyle.mm @@ -226,18 +226,18 @@ static NSURL *MGLStyleURL_emerald; { auto layers = self.mapView.mbglMap->getLayers(); NSMutableArray *styleLayers = [NSMutableArray arrayWithCapacity:layers.size()]; - for (auto layer = layers.rbegin(); layer != layers.rend(); ++layer) { - MGLStyleLayer *styleLayer = [self layerFromMBGLLayer:*layer]; + for (auto layer : layers) { + MGLStyleLayer *styleLayer = [self layerFromMBGLLayer:layer]; [styleLayers addObject:styleLayer]; } return styleLayers; } - (void)setLayers:(NS_MUTABLE_ARRAY_OF(MGLStyleLayer *) *)layers { - for (MGLStyleLayer *layer in self.layers.reverseObjectEnumerator) { + for (MGLStyleLayer *layer in self.layers) { [self removeLayer:layer]; } - for (MGLStyleLayer *layer in layers.reverseObjectEnumerator) { + for (MGLStyleLayer *layer in layers) { [self addLayer:layer]; } } @@ -250,12 +250,12 @@ static NSURL *MGLStyleURL_emerald; - (MGLStyleLayer *)objectInLayersAtIndex:(NSUInteger)index { auto layers = self.mapView.mbglMap->getLayers(); - if (index > layers.size() - 1) { + if (index >= layers.size()) { [NSException raise:NSRangeException format:@"No style layer at index %lu.", (unsigned long)index]; return nil; } - auto layer = layers.at(layers.size() - 1 - index); + auto layer = layers.at(index); return [self layerFromMBGLLayer:layer]; } @@ -287,13 +287,14 @@ static NSURL *MGLStyleURL_emerald; format:@"Cannot insert style layer at out-of-bounds index %lu.", (unsigned long)index]; } else if (index == 0) { try { - [styleLayer addToMapView:self.mapView belowLayer:nil]; + MGLStyleLayer *sibling = layers.size() ? [self layerFromMBGLLayer:layers.at(0)] : nil; + [styleLayer addToMapView:self.mapView belowLayer:sibling]; } catch (const std::runtime_error & err) { [NSException raise:@"MGLRedundantLayerIdentifierException" format:@"%s", err.what()]; } } else { try { - MGLStyleLayer *sibling = [self layerFromMBGLLayer:layers.at(layers.size() - index)]; + MGLStyleLayer *sibling = [self layerFromMBGLLayer:layers.at(index)]; [styleLayer addToMapView:self.mapView belowLayer:sibling]; } catch (std::runtime_error & err) { [NSException raise:@"MGLRedundantLayerIdentifierException" format:@"%s", err.what()]; @@ -304,11 +305,11 @@ static NSURL *MGLStyleURL_emerald; - (void)removeObjectFromLayersAtIndex:(NSUInteger)index { auto layers = self.mapView.mbglMap->getLayers(); - if (index > layers.size() - 1) { + if (index >= layers.size()) { [NSException raise:NSRangeException format:@"Cannot remove style layer at out-of-bounds index %lu.", (unsigned long)index]; } - auto layer = layers.at(layers.size() - 1 - index); + auto layer = layers.at(index); MGLStyleLayer *styleLayer = [self layerFromMBGLLayer:layer]; [styleLayer removeFromMapView:self.mapView]; } diff --git a/platform/darwin/test/MGLStyleTests.mm b/platform/darwin/test/MGLStyleTests.mm index c67497640..55a5df08c 100644 --- a/platform/darwin/test/MGLStyleTests.mm +++ b/platform/darwin/test/MGLStyleTests.mm @@ -217,4 +217,36 @@ XCTAssertEqual(image.size.height, styleImage.size.height); } +- (void)testLayersOrder { + [self.mapView setStyleURL:[MGLStyle streetsStyleURLWithVersion:8]]; + + NSString *filePath = [[NSBundle bundleForClass:self.class] pathForResource:@"amsterdam" ofType:@"geojson"]; + NSURL *url = [NSURL fileURLWithPath:filePath]; + MGLShapeSource *source = [[MGLShapeSource alloc] initWithIdentifier:@"sourceID" URL:url options:nil]; + [self.mapView.style addSource:source]; + + MGLCircleStyleLayer *layer1 = [[MGLCircleStyleLayer alloc] initWithIdentifier:@"layer1" source:source]; + [self.mapView.style addLayer:layer1]; + + MGLCircleStyleLayer *layer3 = [[MGLCircleStyleLayer alloc] initWithIdentifier:@"layer3" source:source]; + [self.mapView.style addLayer:layer3]; + + MGLCircleStyleLayer *layer2 = [[MGLCircleStyleLayer alloc] initWithIdentifier:@"layer2" source:source]; + [self.mapView.style insertLayer:layer2 aboveLayer:layer1]; + + MGLCircleStyleLayer *layer4 = [[MGLCircleStyleLayer alloc] initWithIdentifier:@"layer4" source:source]; + [self.mapView.style insertLayer:layer4 aboveLayer:layer3]; + + MGLCircleStyleLayer *layer0 = [[MGLCircleStyleLayer alloc] initWithIdentifier:@"layer0" source:source]; + [self.mapView.style insertLayer:layer0 belowLayer:layer1]; + + NSArray *layers = [self.mapView.style layers]; + + 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]); +} + @end