summaryrefslogtreecommitdiff
path: root/platform
diff options
context:
space:
mode:
authorThiago Marcos P. Santos <thiago@mapbox.com>2016-12-13 19:48:18 -0200
committerThiago Marcos P. Santos <tmpsantos@gmail.com>2016-12-14 14:21:20 -0200
commit8f24119bd0eb8e5e03f9d8c4422c5bcb3baeafce (patch)
tree5a45a926948edf08b53129f35ef20fc25fdadc52 /platform
parent070a9b43e1af240968f7db6447d4443577aced4a (diff)
downloadqtlocation-mapboxgl-8f24119bd0eb8e5e03f9d8c4422c5bcb3baeafce.tar.gz
[Qt] Implement updateSource
It will create or update a source. Right now only GeoJSON sources are mutable and can be updated using this method.
Diffstat (limited to 'platform')
-rw-r--r--platform/qt/include/qmapboxgl.hpp1
-rw-r--r--platform/qt/src/qmapboxgl.cpp28
-rw-r--r--platform/qt/src/qquickmapboxglrenderer.cpp2
3 files changed, 28 insertions, 3 deletions
diff --git a/platform/qt/include/qmapboxgl.hpp b/platform/qt/include/qmapboxgl.hpp
index 2361b78f8f..8a77bbffab 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 75f5d112ca..ae4c028eed 100644
--- a/platform/qt/src/qmapboxgl.cpp
+++ b/platform/qt/src/qmapboxgl.cpp
@@ -13,6 +13,7 @@
#include <mbgl/style/conversion/layer.hpp>
#include <mbgl/style/conversion/source.hpp>
#include <mbgl/style/layers/custom_layer.hpp>
+#include <mbgl/style/sources/geojson_source.hpp>
#include <mbgl/style/transition_options.hpp>
#include <mbgl/sprite/sprite_image.hpp>
#include <mbgl/storage/network_status.hpp>
@@ -694,8 +695,6 @@ void QMapboxGL::addSource(const QString &sourceID, const QVariantMap &params)
using namespace mbgl::style;
using namespace mbgl::style::conversion;
- removeSource(sourceID);
-
Result<std::unique_ptr<Source>> source = convert<std::unique_ptr<Source>>(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 &params)
d_ptr->mapObj->addSource(std::move(*source));
}
+void QMapboxGL::updateSource(const QString &sourceID, const QVariantMap &params)
+{
+ 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<GeoJSONSource>();
+ 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 903e1c0b05..880d7b2e40 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();