summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruno de Oliveira Abinader <bruno@mapbox.com>2017-02-07 17:52:21 +0200
committerBruno de Oliveira Abinader <bruno@mapbox.com>2017-02-08 15:55:47 +0200
commitd3ca6c67f1b8fbf5d71c307e71ba89a483ec75bc (patch)
tree194565cdc8bf7f497eb7c157b35e73042551ea2e
parent7db79da50185a471f77ebed52575c3bc212c7d7d (diff)
downloadqtlocation-mapboxgl-d3ca6c67f1b8fbf5d71c307e71ba89a483ec75bc.tar.gz
[Qt] Added QMapbox::Feature → GeoJSON conversion helper
-rw-r--r--platform/qt/app/mapwindow.cpp24
-rw-r--r--platform/qt/src/qt_conversion.hpp8
-rw-r--r--platform/qt/src/qt_geojson.hpp15
3 files changed, 44 insertions, 3 deletions
diff --git a/platform/qt/app/mapwindow.cpp b/platform/qt/app/mapwindow.cpp
index 050b498503..e29e62f157 100644
--- a/platform/qt/app/mapwindow.cpp
+++ b/platform/qt/app/mapwindow.cpp
@@ -258,6 +258,30 @@ void MapWindow::keyPressEvent(QKeyEvent *ev)
}
}
break;
+ case Qt::Key_5: {
+ if (m_map->layerExists("circleLayer")) {
+ m_map->removeLayer("circleLayer");
+ m_map->removeSource("circleSource");
+ } else {
+ QMapbox::CoordinatesCollections geometry { { { m_map->coordinate() } } };
+ QMapbox::Feature feature { QMapbox::Feature::PointType, geometry, {}, {} };
+
+ QVariantMap circleSource;
+ circleSource["type"] = "geojson";
+ circleSource["data"] = QVariant::fromValue<QMapbox::Feature>(feature);
+ m_map->addSource("circleSource", circleSource);
+
+ QVariantMap circle;
+ circle["id"] = "circleLayer";
+ circle["type"] = "circle";
+ circle["source"] = "circleSource";
+ m_map->addLayer(circle);
+
+ m_map->setPaintProperty("circleLayer", "circle-radius", 10.0);
+ m_map->setPaintProperty("circleLayer", "circle-color", QColor("black"));
+ }
+ }
+ break;
case Qt::Key_Tab:
m_map->cycleDebugOptions();
break;
diff --git a/platform/qt/src/qt_conversion.hpp b/platform/qt/src/qt_conversion.hpp
index ae05200d66..4b93ca7423 100644
--- a/platform/qt/src/qt_conversion.hpp
+++ b/platform/qt/src/qt_conversion.hpp
@@ -30,7 +30,13 @@ inline QVariant arrayMember(const QVariant& value, std::size_t i) {
}
inline bool isObject(const QVariant& value) {
- return value.canConvert(QVariant::Map) || value.type() == QVariant::ByteArray;
+ return value.canConvert(QVariant::Map)
+ || value.type() == QVariant::ByteArray
+#if QT_VERSION >= 0x050000
+ || QString(value.typeName()) == QStringLiteral("QMapbox::Feature");
+#else
+ || QString(value.typeName()) == QString("QMapbox::Feature");
+#endif
}
inline optional<QVariant> objectMember(const QVariant& value, const char* key) {
diff --git a/platform/qt/src/qt_geojson.hpp b/platform/qt/src/qt_geojson.hpp
index caec790842..038b051941 100644
--- a/platform/qt/src/qt_geojson.hpp
+++ b/platform/qt/src/qt_geojson.hpp
@@ -180,8 +180,19 @@ namespace style {
namespace conversion {
template <>
+Result<GeoJSON> convertGeoJSON(const QMapbox::Feature& feature) {
+ return Result<GeoJSON> { GeoJSON { asMapboxGLFeature(feature) } };
+}
+
+template <>
Result<GeoJSON> convertGeoJSON(const QVariant& value) {
- if (value.type() != QVariant::ByteArray) {
+#if QT_VERSION >= 0x050000
+ if (value.typeName() == QStringLiteral("QMapbox::Feature")) {
+#else
+ if (value.typeName() == QString("QMapbox::Feature")) {
+#endif
+ return convertGeoJSON(value.value<QMapbox::Feature>());
+ } else if (value.type() != QVariant::ByteArray) {
return Error { "JSON data must be in QByteArray" };
}
@@ -201,7 +212,7 @@ Result<GeoJSON> convertGeoJSON(const QVariant& value) {
return Error { message.str() };
}
- conversion::Result<GeoJSON> geoJSON = conversion::convertGeoJSON<JSValue>(d);
+ Result<GeoJSON> geoJSON = convertGeoJSON<JSValue>(d);
if (!geoJSON) {
return Error { geoJSON.error().message };
}