diff --git a/src/mbgl/annotation/annotation_manager.cpp b/src/mbgl/annotation/annotation_manager.cpp index 5e589bf28..45fb1218f 100644 --- a/src/mbgl/annotation/annotation_manager.cpp +++ b/src/mbgl/annotation/annotation_manager.cpp @@ -51,7 +51,7 @@ void AnnotationManager::removeAnnotations(const AnnotationIDs& ids) { pointTree.remove(pointAnnotations.at(id)); pointAnnotations.erase(id); } else if (shapeAnnotations.find(id) != shapeAnnotations.end()) { - // TODO: remove style layer + obsoleteShapeAnnotationLayers.push_back(shapeAnnotations.at(id)->layerID); shapeAnnotations.erase(id); } } @@ -132,6 +132,11 @@ void AnnotationManager::updateStyle(Style& style) { shape.second->updateStyle(style); } + for (const auto& layer : obsoleteShapeAnnotationLayers) { + style.removeLayer(layer); + } + + obsoleteShapeAnnotationLayers.clear(); style.getSource(SourceID)->invalidateTiles(); } diff --git a/src/mbgl/annotation/annotation_manager.hpp b/src/mbgl/annotation/annotation_manager.hpp index 1251585b0..42acbb998 100644 --- a/src/mbgl/annotation/annotation_manager.hpp +++ b/src/mbgl/annotation/annotation_manager.hpp @@ -40,6 +40,7 @@ private: PointAnnotationImpl::Tree pointTree; PointAnnotationImpl::Map pointAnnotations; ShapeAnnotationImpl::Map shapeAnnotations; + std::vector obsoleteShapeAnnotationLayers; }; } diff --git a/src/mbgl/annotation/shape_annotation_impl.hpp b/src/mbgl/annotation/shape_annotation_impl.hpp index 6e00a50e3..19eb05f89 100644 --- a/src/mbgl/annotation/shape_annotation_impl.hpp +++ b/src/mbgl/annotation/shape_annotation_impl.hpp @@ -24,10 +24,11 @@ public: void updateStyle(Style&); void updateTile(const TileID&, AnnotationTile&); -private: const AnnotationID id; const std::string layerID; const ShapeAnnotation shape; + +private: mapbox::util::geojsonvt::GeoJSONVT shapeTiler; }; diff --git a/src/mbgl/style/style.cpp b/src/mbgl/style/style.cpp index 5d1a9a1a6..8859475e4 100644 --- a/src/mbgl/style/style.cpp +++ b/src/mbgl/style/style.cpp @@ -77,12 +77,30 @@ void Style::addSource(std::unique_ptr source) { sources.emplace_back(std::move(source)); } +std::vector>::const_iterator Style::findLayer(const std::string& id) const { + return std::find_if(layers.begin(), layers.end(), [&](const auto& layer) { + return layer->id == id; + }); +} + +StyleLayer* Style::getLayer(const std::string& id) const { + auto it = findLayer(id); + return it != layers.end() ? it->get() : nullptr; +} + void Style::addLayer(util::ptr layer) { layers.emplace_back(std::move(layer)); } void Style::addLayer(util::ptr layer, const std::string& before) { - layers.emplace(std::find_if(layers.begin(), layers.end(), [&](const auto& l) { return l->id == before; }), std::move(layer)); + layers.emplace(findLayer(before), std::move(layer)); +} + +void Style::removeLayer(const std::string& id) { + auto it = findLayer(id); + if (it == layers.end()) + throw std::runtime_error("no such layer"); + layers.erase(it); } void Style::update(const TransformState& transform, @@ -141,14 +159,6 @@ Source* Style::getSource(const std::string& id) const { return it != sources.end() ? it->get() : nullptr; } -StyleLayer* Style::getLayer(const std::string& id) const { - const auto it = std::find_if(layers.begin(), layers.end(), [&](const auto& layer) { - return layer->id == id; - }); - - return it != layers.end() ? it->get() : nullptr; -} - bool Style::hasTransitions() const { for (const auto& layer : layers) { if (layer->hasTransitions()) { diff --git a/src/mbgl/style/style.hpp b/src/mbgl/style/style.hpp index 9c5091863..f4b2aac48 100644 --- a/src/mbgl/style/style.hpp +++ b/src/mbgl/style/style.hpp @@ -67,6 +67,7 @@ public: void addSource(std::unique_ptr); void addLayer(util::ptr); void addLayer(util::ptr, const std::string& beforeLayerID); + void removeLayer(const std::string& layerID); MapData& data; std::unique_ptr glyphStore; @@ -80,6 +81,8 @@ public: std::vector> layers; private: + std::vector>::const_iterator findLayer(const std::string& layerID) const; + // GlyphStore::Observer implementation. void onGlyphRangeLoaded() override; void onGlyphRangeLoadingFailed(std::exception_ptr error) override; diff --git a/test/api/annotations.cpp b/test/api/annotations.cpp index 84cf259b0..0918d7893 100644 --- a/test/api/annotations.cpp +++ b/test/api/annotations.cpp @@ -120,7 +120,7 @@ TEST(Annotations, NonImmediateAdd) { util::write_file("test/output/non_immediate_add.png", renderPNG(map)); } -TEST(Annotations, Remove) { +TEST(Annotations, RemovePoint) { auto display = std::make_shared(); HeadlessView view(display, 1); DefaultFileSource fileSource(nullptr); @@ -133,7 +133,32 @@ TEST(Annotations, Remove) { map.removeAnnotation(point); - util::write_file("test/output/remove.png", renderPNG(map)); + util::write_file("test/output/remove_point.png", renderPNG(map)); +} + +TEST(Annotations, RemoveShape) { + auto display = std::make_shared(); + HeadlessView view(display, 1); + DefaultFileSource fileSource(nullptr); + + AnnotationSegments segments = {{ {{ { 0, 0 }, { 45, 45 } }} }}; + + LineProperties lineProperties; + lineProperties.color = {{ 255, 0, 0, 1 }}; + lineProperties.width = 5; + + StyleProperties styleProperties; + styleProperties.set(lineProperties); + + Map map(view, fileSource, MapMode::Still); + map.setStyleJSON(util::read_file("test/fixtures/api/empty.json"), ""); + uint32_t shape = map.addShapeAnnotation(ShapeAnnotation(segments, styleProperties)); + + renderPNG(map); + + map.removeAnnotation(shape); + + util::write_file("test/output/remove_shape.png", renderPNG(map)); } TEST(Annotations, SwitchStyle) {