[Qt] Added QMapbox::Feature → GeoJSON conversion helper
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -179,9 +179,20 @@ namespace mbgl {
|
||||
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 };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user