From 8f24119bd0eb8e5e03f9d8c4422c5bcb3baeafce Mon Sep 17 00:00:00 2001 From: "Thiago Marcos P. Santos" Date: Tue, 13 Dec 2016 19:48:18 -0200 Subject: [PATCH] [Qt] Implement updateSource It will create or update a source. Right now only GeoJSON sources are mutable and can be updated using this method. --- platform/qt/include/qmapboxgl.hpp | 1 + platform/qt/src/qmapboxgl.cpp | 28 ++++++++++++++++++++-- platform/qt/src/qquickmapboxglrenderer.cpp | 2 +- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/platform/qt/include/qmapboxgl.hpp b/platform/qt/include/qmapboxgl.hpp index 2361b78f8..8a77bbffa 100644 --- a/platform/qt/include/qmapboxgl.hpp +++ b/platform/qt/include/qmapboxgl.hpp @@ -186,6 +186,7 @@ public: QMargins margins() const; void addSource(const QString &sourceID, const QVariantMap& params); + void updateSource(const QString &sourceID, const QVariantMap& params); void removeSource(const QString &sourceID); void addImage(const QString &name, const QImage &sprite); diff --git a/platform/qt/src/qmapboxgl.cpp b/platform/qt/src/qmapboxgl.cpp index 75f5d112c..ae4c028ee 100644 --- a/platform/qt/src/qmapboxgl.cpp +++ b/platform/qt/src/qmapboxgl.cpp @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -694,8 +695,6 @@ void QMapboxGL::addSource(const QString &sourceID, const QVariantMap ¶ms) using namespace mbgl::style; using namespace mbgl::style::conversion; - removeSource(sourceID); - Result> source = convert>(QVariant(params), sourceID.toStdString()); if (!source) { qWarning() << "Unable to add source:" << source.error().message.c_str(); @@ -705,6 +704,31 @@ void QMapboxGL::addSource(const QString &sourceID, const QVariantMap ¶ms) d_ptr->mapObj->addSource(std::move(*source)); } +void QMapboxGL::updateSource(const QString &sourceID, const QVariantMap ¶ms) +{ + using namespace mbgl::style; + using namespace mbgl::style::conversion; + + auto source = d_ptr->mapObj->getSource(sourceID.toStdString()); + if (!source) { + addSource(sourceID, params); + return; + } + + auto sourceGeoJSON = source->as(); + if (!sourceGeoJSON) { + qWarning() << "Unable to update source: only GeoJSON sources are mutable."; + return; + } + + if (params.contains("data")) { + auto result = convertGeoJSON(params["data"]); + if (result) { + sourceGeoJSON->setGeoJSON(*result); + } + } +} + void QMapboxGL::removeSource(const QString& sourceID) { auto sourceIDStdString = sourceID.toStdString(); diff --git a/platform/qt/src/qquickmapboxglrenderer.cpp b/platform/qt/src/qquickmapboxglrenderer.cpp index 903e1c0b0..880d7b2e4 100644 --- a/platform/qt/src/qquickmapboxglrenderer.cpp +++ b/platform/qt/src/qquickmapboxglrenderer.cpp @@ -88,7 +88,7 @@ void QQuickMapboxGLRenderer::synchronize(QQuickFramebufferObject *item) } for (const auto& change : quickMap->m_sourceChanges) { - m_map->addSource(change.value("id").toString(), change); + m_map->updateSource(change.value("id").toString(), change); } quickMap->m_sourceChanges.clear();