From 6ba14c6d2a6b7eb325cd975393306c4fe6809acd Mon Sep 17 00:00:00 2001 From: Paolo Angelelli Date: Mon, 18 Sep 2017 11:43:29 +0200 Subject: Fix Map.fromCoordinate returning wrong values during tilting Task-number: QTBUG-63251 Change-Id: I7ceeeebb30007c5d97d61e057c8ec6e827177b19 Reviewed-by: Eskil Abrahamsen Blomfeldt --- src/location/maps/qgeoprojection.cpp | 20 +++++++++++++++----- tests/auto/declarative_ui/tst_map.qml | 22 ++++++++++++++++++++++ 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/src/location/maps/qgeoprojection.cpp b/src/location/maps/qgeoprojection.cpp index 013a8c33..fcf9a350 100644 --- a/src/location/maps/qgeoprojection.cpp +++ b/src/location/maps/qgeoprojection.cpp @@ -225,6 +225,9 @@ QDoubleVector2D QGeoProjectionWebMercator::itemPositionToWrappedMapProjection(co /* Default implementations */ QGeoCoordinate QGeoProjectionWebMercator::itemPositionToCoordinate(const QDoubleVector2D &pos, bool clipToViewport) const { + if (qIsNaN(pos.x()) || qIsNaN(pos.y())) + return QGeoCoordinate(); + if (clipToViewport) { int w = m_viewportWidth; int h = m_viewportHeight; @@ -242,7 +245,14 @@ QGeoCoordinate QGeoProjectionWebMercator::itemPositionToCoordinate(const QDouble QDoubleVector2D QGeoProjectionWebMercator::coordinateToItemPosition(const QGeoCoordinate &coordinate, bool clipToViewport) const { - QDoubleVector2D pos = wrappedMapProjectionToItemPosition(wrapMapProjection(geoToMapProjection(coordinate))); + if (!coordinate.isValid()) + return QDoubleVector2D(qQNaN(), qQNaN()); + + QDoubleVector2D wrappedProjection = wrapMapProjection(geoToMapProjection(coordinate)); + if (!isProjectable(wrappedProjection)) + return QDoubleVector2D(qQNaN(), qQNaN()); + + QDoubleVector2D pos = wrappedMapProjectionToItemPosition(wrappedProjection); if (clipToViewport) { int w = m_viewportWidth; @@ -339,10 +349,10 @@ QDoubleVector2D QGeoProjectionWebMercator::viewportToWrappedMapProjection(const pos *= QDoubleVector2D(m_halfWidth, m_halfHeight); QDoubleVector3D p = m_centerNearPlane; - p -= m_up * pos.y(); - p -= m_side * pos.x(); + p += m_up * pos.y(); + p += m_side * pos.x(); - QDoubleVector3D ray = p - m_eye; + QDoubleVector3D ray = m_eye - p; ray.normalize(); return (xyPlane.lineIntersection(m_eye, ray, s) / m_sideLength).toVector2D(); @@ -461,7 +471,7 @@ void QGeoProjectionWebMercator::setupCamera() m_quickItemTransformation = m_transformation; m_transformation.scale(m_sideLength, m_sideLength, 1.0); - m_centerNearPlane = m_eye + m_viewNormalized; + m_centerNearPlane = m_eye - m_viewNormalized; m_centerNearPlaneMercator = m_eyeMercator - m_viewNormalized * m_nearPlaneMercator; // The method does not support tilting angles >= 90.0 or < 0. diff --git a/tests/auto/declarative_ui/tst_map.qml b/tests/auto/declarative_ui/tst_map.qml index c36ebc0e..8821f55e 100644 --- a/tests/auto/declarative_ui/tst_map.qml +++ b/tests/auto/declarative_ui/tst_map.qml @@ -72,6 +72,7 @@ Item { && coordinateMap.mapReady && mapTiltBearing.mapReady && mapTiltBearingHere.mapReady + && mapTestProjection.mapReady Map { id: mapZoomOnCompleted; width: 200; height: 200; zoomLevel: 3; center: coordinate1; plugin: testPlugin; @@ -113,6 +114,13 @@ Item { plugin: testPluginLazyParameter } + Map { + id: mapTestProjection + plugin: testPlugin + width: 200 + height: 200 + } + MapParameter { id: testParameter type: "cameraCenter_test" @@ -593,6 +601,20 @@ Item { coord = coordinateMap.toCoordinate(Qt.point(-5, -6)) verify(isNaN(coord.latitude)) verify(isNaN(coord.longitde)) + + // test with tilting + coord = QtPositioning.coordinate(45.6, 17.67) + var pos = mapTestProjection.fromCoordinate(coord, false) + compare(Math.floor(pos.x), 3339) + compare(Math.floor(pos.y), 1727) + mapTestProjection.tilt = 6 + pos = mapTestProjection.fromCoordinate(coord, false) + compare(Math.floor(pos.x), 11066) + compare(Math.floor(pos.y), 5577) + mapTestProjection.tilt = 12 + pos = mapTestProjection.fromCoordinate(coord, false) + verify(isNaN(pos.latitude)) + verify(isNaN(pos.longitde)) } } } -- cgit v1.2.1