diff --git a/platform/qt/include/qmapbox.hpp b/platform/qt/include/qmapbox.hpp index 8877d730b..fc52218bb 100644 --- a/platform/qt/include/qmapbox.hpp +++ b/platform/qt/include/qmapbox.hpp @@ -13,6 +13,8 @@ typedef QPair Coordinate; typedef QList Coordinates; typedef QList CoordinateSegments; +typedef QPair CoordinateZoom; + typedef quint32 AnnotationID; typedef QList AnnotationIDs; diff --git a/platform/qt/include/qmapboxgl.hpp b/platform/qt/include/qmapboxgl.hpp index 01538268d..f3e0af98e 100644 --- a/platform/qt/include/qmapboxgl.hpp +++ b/platform/qt/include/qmapboxgl.hpp @@ -158,6 +158,9 @@ public: QPointF pixelForCoordinate(const QMapbox::Coordinate &) const; QMapbox::Coordinate coordinateForPixel(const QPointF &) const; + QMapbox::CoordinateZoom coordinateZoomForBounds(const QMapbox::Coordinate &sw, QMapbox::Coordinate &ne) const; + QMapbox::CoordinateZoom coordinateZoomForBounds(const QMapbox::Coordinate &sw, QMapbox::Coordinate &ne, double bearing, double pitch); + void setMargins(const QMargins &margins); QMargins margins() const; diff --git a/platform/qt/src/qmapboxgl.cpp b/platform/qt/src/qmapboxgl.cpp index ed6f8566a..e7752df29 100644 --- a/platform/qt/src/qmapboxgl.cpp +++ b/platform/qt/src/qmapboxgl.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -460,6 +461,35 @@ Coordinate QMapboxGL::coordinateForPixel(const QPointF &pixel) const return Coordinate(latLng.latitude, latLng.longitude); } +CoordinateZoom QMapboxGL::coordinateZoomForBounds(const Coordinate &sw, Coordinate &ne) const +{ + auto bounds = mbgl::LatLngBounds::hull(mbgl::LatLng { sw.first, sw.second }, mbgl::LatLng { ne.first, ne.second }); + mbgl::CameraOptions camera = d_ptr->mapObj->cameraForLatLngBounds(bounds, d_ptr->margins); + + return {{ (*camera.center).latitude, (*camera.center).longitude }, *camera.zoom }; +} + +CoordinateZoom QMapboxGL::coordinateZoomForBounds(const Coordinate &sw, Coordinate &ne, + double newBearing, double newPitch) +{ + // FIXME: mbgl::Map::cameraForLatLngBounds should + // take bearing and pitch as input too, so this + // hack won't be needed. + double currentBearing = bearing(); + double currentPitch = pitch(); + + setBearing(newBearing); + setPitch(newPitch); + + auto bounds = mbgl::LatLngBounds::hull(mbgl::LatLng { sw.first, sw.second }, mbgl::LatLng { ne.first, ne.second }); + mbgl::CameraOptions camera = d_ptr->mapObj->cameraForLatLngBounds(bounds, d_ptr->margins); + + setBearing(currentBearing); + setPitch(currentPitch); + + return {{ (*camera.center).latitude, (*camera.center).longitude }, *camera.zoom }; +} + void QMapboxGL::setMargins(const QMargins &margins_) { d_ptr->margins = {