[Qt] Implement updateSource

It will create or update a source. Right now only GeoJSON sources are
mutable and can be updated using this method.
This commit is contained in:
Thiago Marcos P. Santos
2016-12-13 19:48:18 -02:00
committed by Thiago Marcos P. Santos
parent 070a9b43e1
commit 8f24119bd0
3 changed files with 28 additions and 3 deletions

View File

@@ -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);

View File

@@ -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();

View File

@@ -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();