Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

[macos, ios] Insert layer above #6730

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/mbgl/map/map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ class Map : private util::noncopyable {
// Layers
style::Layer* getLayer(const std::string& layerID);
void addLayer(std::unique_ptr<style::Layer>, const optional<std::string>& beforeLayerID = {});
Copy link
Contributor Author

@frederoni frederoni Oct 18, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should I remove the optional beforeLayerID and combine beforeOrAfter in the insert method?

void insertLayer(std::unique_ptr<style::Layer>, const std::string& afterLayerID);
void removeLayer(const std::string& layerID);

// Add image, bound to the style
Expand Down
8 changes: 8 additions & 0 deletions platform/darwin/src/MGLStyle.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,14 @@ static const NSInteger MGLStyleDefaultVersion = 9;
*/
- (void)insertLayer:(MGLStyleLayer *)layer belowLayer:(MGLStyleLayer *)otherLayer;

/**
Inserts a new layer above another layer.

@param layer Layer to be inserted.
@param aboveLayer A layer that's already on the map view.
*/
- (void)insertLayer:(MGLStyleLayer *)layer aboveLayer:(MGLStyleLayer *)otherLayer;

/**
Removes a layer from the map view.

Expand Down
21 changes: 21 additions & 0 deletions platform/darwin/src/MGLStyle.mm
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,27 @@ - (void)insertLayer:(MGLStyleLayer *)layer belowLayer:(MGLStyleLayer *)otherLaye
self.mapView.mbglMap->addLayer(std::unique_ptr<mbgl::style::Layer>(layer.layer), belowLayerId);
}

- (void)insertLayer:(MGLStyleLayer *)layer aboveLayer:(MGLStyleLayer *)otherLayer
{
if (!layer.layer) {
[NSException raise:NSInvalidArgumentException
format:
@"The style layer %@ cannot be added to the style. "
@"Make sure the style layer was created as a member of a concrete subclass of MGLStyleLayer.",
NSStringFromClass(layer)];
}
if (!otherLayer.layer) {
[NSException raise:NSInvalidArgumentException
format:
@"A style layer cannot be placed before %@ in the style. "
@"Make sure the style layer was created as a member of a concrete subclass of MGLStyleLayer.",
NSStringFromClass(otherLayer)];
}

auto afterLayerID = otherLayer.layer->getID();
self.mapView.mbglMap->insertLayer(std::unique_ptr<mbgl::style::Layer>(layer.layer), afterLayerID);
}

- (void)addSource:(MGLSource *)source
{
self.mapView.mbglMap->addSource(source.mbglSource);
Expand Down
16 changes: 16 additions & 0 deletions platform/ios/app/MBXViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -676,10 +676,26 @@ - (void)styleRoadLayer
roadLayer.lineColor = roadLineColor;
roadLayer.lineWidth = lineWidthFunction;
roadLayer.lineGapWidth = lineWidthFunction;

MGLLineCap lineCap = MGLLineCapRound;
NSValue *lineCapValue = [NSValue value:&lineCap withObjCType:@encode(MGLLineCap)];
roadLayer.lineCap = [MGLStyleValue<NSValue *> valueWithRawValue:lineCapValue];

roadLayer.visible = YES;
roadLayer.maximumZoomLevel = 15;
roadLayer.minimumZoomLevel = 13;

NSString *filePath = [[NSBundle bundleForClass:self.class] pathForResource:@"polyline" ofType:@"geojson"];
NSURL *geoJSONURL = [NSURL fileURLWithPath:filePath];
MGLGeoJSONSource *source = [[MGLGeoJSONSource alloc] initWithIdentifier:@"ams" URL:geoJSONURL options:nil];
[self.mapView.style addSource:source];

MGLLineStyleLayer *topMostLayer = (MGLLineStyleLayer *)[self.mapView.style layerWithIdentifier:@"road-trunk"];
MGLLineStyleLayer *insertLayer = [[MGLLineStyleLayer alloc] initWithIdentifier:@"testLayer" source:source];
insertLayer.lineColor = [MGLStyleValue<UIColor *> valueWithRawValue:[UIColor greenColor]];
insertLayer.lineWidth = topMostLayer.lineWidth;

[self.mapView.style insertLayer:insertLayer aboveLayer:topMostLayer];
}

- (void)styleRasterLayer
Expand Down
14 changes: 14 additions & 0 deletions src/mbgl/map/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,20 @@ void Map::addLayer(std::unique_ptr<Layer> layer, const optional<std::string>& be

impl->view.deactivate();
}

void Map::insertLayer(std::unique_ptr<Layer> layer, const std::string &after) {
if (!impl->style) {
return;
}

impl->styleMutated = true;
impl->view.activate();

impl->style->insertLayer(std::move(layer), after);
update(Update::Classes);

impl->view.deactivate();
}

void Map::removeLayer(const std::string& id) {
if (!impl->style) {
Expand Down
25 changes: 25 additions & 0 deletions src/mbgl/style/style.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,31 @@ Layer* Style::addLayer(std::unique_ptr<Layer> layer, optional<std::string> befor
return layers.emplace(before ? findLayer(*before) : layers.end(), std::move(layer))->get();
}

Layer* Style::insertLayer(std::unique_ptr<Layer> layer, const std::string &after) {

if (SymbolLayer* symbolLayer = layer->as<SymbolLayer>()) {
if (!symbolLayer->impl->spriteAtlas) {
symbolLayer->impl->spriteAtlas = spriteAtlas.get();
}
}

if (CustomLayer* customLayer = layer->as<CustomLayer>()) {
customLayer->impl->initialize();
}

layer->baseImpl->setObserver(this);

auto it = std::find_if(layers.begin(), layers.end(), [&](const auto& fl) {
return fl->baseImpl->id == after;
});

if (it != layers.end()) {
it++;
}

return layers.insert(it, std::move(layer))->get();
}

void Style::removeLayer(const std::string& id) {
auto it = findLayer(id);
if (it == layers.end())
Expand Down
2 changes: 2 additions & 0 deletions src/mbgl/style/style.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ class Style : public GlyphAtlasObserver,
Layer* getLayer(const std::string& id) const;
Layer* addLayer(std::unique_ptr<Layer>,
optional<std::string> beforeLayerID = {});
Layer* insertLayer(std::unique_ptr<Layer>,
const std::string& afterLayerID);
void removeLayer(const std::string& layerID);

std::string getName() const;
Expand Down
7 changes: 7 additions & 0 deletions test/api/custom_layer.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,16 @@ TEST(CustomLayer, Basic) {
}, new TestLayer()));

auto layer = std::make_unique<FillLayer>("landcover", "mapbox");
std::string layerID = layer->getID();
layer->setSourceLayer("landcover");
layer->setFillColor(Color{ 1.0, 1.0, 0.0, 1.0 });
map.addLayer(std::move(layer));

auto insertLayer = std::make_unique<FillLayer>("landcover", "mapbox");
insertLayer->setSourceLayer("landcover");
insertLayer->setFillColor(Color{ 0.0, 0.0, 0.0, 0.0 });
insertLayer->setFillOutlineColor(Color{ 0.0, 0.0, 1, 1.0 });
map.insertLayer(std::move(insertLayer), layerID);

test::checkImage("test/fixtures/custom_layer/basic", test::render(map), 0.0006, 0.1);
}
Binary file modified test/fixtures/custom_layer/basic/expected.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.