From e92bf21dfb0de4595a39e172fdbdc16b8ae8579c Mon Sep 17 00:00:00 2001 From: Paolo Angelelli Date: Tue, 6 Jun 2017 16:01:17 +0200 Subject: Add alignCoordinateToPoint method This method effectively extends the functionalities offered by the center qml property, allowing to set a coordinate to an item position other than the center of the item. This is useful in those applications where the center of the scene (e.g., a cursor) is not to be place exactly in the center of the map. [ChangeLog][QtLocation][QDeclarativeGeoMap] Added alignCoordinateToPoint method to QDeclarativeGeoMap. Change-Id: I68a3d12e8376b83c4c1775a3af5e2125d3e71d63 Reviewed-by: Alex Blasche --- .../declarativemaps/qdeclarativegeomap.cpp | 24 ++++++++++++++++++++ .../declarativemaps/qdeclarativegeomap_p.h | 1 + tests/auto/declarative_ui/tst_map.qml | 26 ++++++++++++++++++++++ 3 files changed, 51 insertions(+) diff --git a/src/location/declarativemaps/qdeclarativegeomap.cpp b/src/location/declarativemaps/qdeclarativegeomap.cpp index 47283d85..67130bb9 100644 --- a/src/location/declarativemaps/qdeclarativegeomap.cpp +++ b/src/location/declarativemaps/qdeclarativegeomap.cpp @@ -1486,6 +1486,30 @@ void QDeclarativeGeoMap::setBearing(qreal bearing, const QGeoCoordinate &coordin setCenter(m_map->geoProjection().anchorCoordinateToPoint(coordinate, rotationPoint)); } +/*! + \qmlmethod void QtLocation::Map::alignCoordinateToPoint(coordinate coordinate, QPointF point) + + Aligns \a coordinate to \a point. + This method effectively extends the functionality offered by the \l center qml property, allowing + to align a coordinate to point of the Map element other than its center. + This is useful in those applications where the center of the scene (e.g., a cursor) is not to be + placed exactly in the center of the map. + + 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. + + \sa center + + \since 5.10 +*/ +void QDeclarativeGeoMap::alignCoordinateToPoint(const QGeoCoordinate &coordinate, const QPointF &point) +{ + if (!m_map) + return; + + setCenter(m_map->geoProjection().anchorCoordinateToPoint(coordinate, point)); +} + /*! \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 2f20e3ae..f88eb03f 100644 --- a/src/location/declarativemaps/qdeclarativegeomap_p.h +++ b/src/location/declarativemaps/qdeclarativegeomap_p.h @@ -156,6 +156,7 @@ public: QQmlListProperty supportedMapTypes(); Q_INVOKABLE void setBearing(qreal bearing, const QGeoCoordinate &coordinate); + Q_INVOKABLE void alignCoordinateToPoint(const QGeoCoordinate &coordinate, const QPointF &point); 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 18d7c2d3..755da268 100644 --- a/tests/auto/declarative_ui/tst_map.qml +++ b/tests/auto/declarative_ui/tst_map.qml @@ -542,6 +542,32 @@ Item { mapTiltBearing.tilt = 25.0 } + function test_map_align_coordinate_to_point() + { + 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 coord = QtPositioning.coordinate(20,-20) + var point = Qt.point(400, 400) + mapTiltBearing.alignCoordinateToPoint(coord, point) + var coordAfter = mapTiltBearing.toCoordinate(point) + compare(coord.latitude, coordAfter.latitude) + compare(coord.longitude, coordAfter.longitude) + + // 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