From 61776400fe4b18a62fd9b4880fc1e799cf0f2cde Mon Sep 17 00:00:00 2001 From: Paolo Angelelli Date: Tue, 6 Jun 2017 15:37:49 +0200 Subject: Add setBearing overload to rotate around a coordinate This patch adds an overload for setBearing in QDeclarativeGeoMap to rotate the map around a specific QGeoCoordinate. [ChangeLog][QtLocation][QDeclarativeGeoMap] Added setBearing overload to rotate the map around a given QGeoCoordinate. Change-Id: I5d2428d852b0c3537f4a1cdb1925c059788c45c5 Reviewed-by: Alex Blasche --- .../declarativemaps/qdeclarativegeomap.cpp | 27 ++++++++++++++++++++++ .../declarativemaps/qdeclarativegeomap_p.h | 2 ++ tests/auto/declarative_ui/tst_map.qml | 27 ++++++++++++++++++++++ 3 files changed, 56 insertions(+) diff --git a/src/location/declarativemaps/qdeclarativegeomap.cpp b/src/location/declarativemaps/qdeclarativegeomap.cpp index cb991341..47283d85 100644 --- a/src/location/declarativemaps/qdeclarativegeomap.cpp +++ b/src/location/declarativemaps/qdeclarativegeomap.cpp @@ -1459,6 +1459,33 @@ QQmlListProperty QDeclarativeGeoMap::supportedMapTypes() return QQmlListProperty(this, m_supportedMapTypes); } +/*! + \qmlmethod void QtLocation::Map::setBearing(real bearing, coordinate coordinate) + + Sets the bearing for the map to \a bearing, rotating it around \a coordinate. + If the Plugin used for the Map supports bearing, the valid range for \a bearing is between 0 and 360. + If the Plugin used for the Map does not support bearing, or if the map is tilted and \a coordinate happens + to be behind the camera, or if the map is not ready (see \l mapReady), calling this method will have no effect. + + \since 5.10 +*/ +void QDeclarativeGeoMap::setBearing(qreal bearing, const QGeoCoordinate &coordinate) +{ + if (!m_map) + return; + + const QDoubleVector2D coordWrapped = m_map->geoProjection().geoToWrappedMapProjection(coordinate); + if (!m_map->geoProjection().isProjectable(coordWrapped)) + return; + + const QPointF rotationPoint = m_map->geoProjection().wrappedMapProjectionToItemPosition(coordWrapped).toPointF(); + + // First set bearing + setBearing(bearing); + // then reanchor + setCenter(m_map->geoProjection().anchorCoordinateToPoint(coordinate, rotationPoint)); +} + /*! \qmlmethod coordinate QtLocation::Map::toCoordinate(QPointF position, bool clipToViewPort) diff --git a/src/location/declarativemaps/qdeclarativegeomap_p.h b/src/location/declarativemaps/qdeclarativegeomap_p.h index f07a2e7f..2f20e3ae 100644 --- a/src/location/declarativemaps/qdeclarativegeomap_p.h +++ b/src/location/declarativemaps/qdeclarativegeomap_p.h @@ -155,6 +155,8 @@ public: QQmlListProperty supportedMapTypes(); + Q_INVOKABLE void setBearing(qreal bearing, const QGeoCoordinate &coordinate); + Q_INVOKABLE void removeMapItem(QDeclarativeGeoMapItemBase *item); Q_INVOKABLE void addMapItem(QDeclarativeGeoMapItemBase *item); diff --git a/tests/auto/declarative_ui/tst_map.qml b/tests/auto/declarative_ui/tst_map.qml index cd79952c..18d7c2d3 100644 --- a/tests/auto/declarative_ui/tst_map.qml +++ b/tests/auto/declarative_ui/tst_map.qml @@ -515,6 +515,33 @@ Item { compare(mapTiltBearing.tilt, 25.0) } + function test_map_setbearing() + { + var zeroCoord = QtPositioning.coordinate(0,0) + mapTiltBearing.bearing = 0.0 + mapTiltBearing.tilt = 0.0 + mapTiltBearing.zoomLevel = 3 + mapTiltBearing.center = zeroCoord + compare(mapTiltBearing.bearing, 0.0) + compare(mapTiltBearing.tilt, 0.0) + compare(mapTiltBearing.zoomLevel, 3) + compare(mapTiltBearing.center, zeroCoord) + + var fulcrum = QtPositioning.coordinate(20,-20) + var fulcrumPos = mapTiltBearing.fromCoordinate(fulcrum) + var bearing = 90.0 + mapTiltBearing.setBearing(bearing, fulcrum) + var fulcrumPosAfter = mapTiltBearing.fromCoordinate(fulcrum) + compare(mapTiltBearing.bearing, bearing) + compare(fulcrumPos, fulcrumPosAfter) + + // resetting + mapTiltBearing.center = coordinate1 + mapTiltBearing.zoomLevel = 4 + mapTiltBearing.bearing = 45.0 + mapTiltBearing.tilt = 25.0 + } + function test_coordinate_conversion() { wait(1000) -- cgit v1.2.1