diff options
author | Erik Mattsson <erik.mattsson@appello.com> | 2013-09-27 15:44:01 +0200 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2013-10-07 09:43:44 +0200 |
commit | 7ec8919a055e35232b5127e312a525f61b0bc483 (patch) | |
tree | 80436fb4170376af1ffc9f31408db9f0a212358b /src/positioning/qgeorectangle.cpp | |
parent | 7ca062e008db1543519f9258812b0d577d5da5ac (diff) | |
download | qtlocation-7ec8919a055e35232b5127e312a525f61b0bc483.tar.gz |
Added possibility to create a georectangle from a list of coordinates
It will create the smallest possible rectangle that contains all of the
coordinates. This is accessible from qml using the locationsingleton.
The plan is to include the ability to fit the view-port of the map
to a georectangle.
Change-Id: I616ac8ef169f697e53a3d9ebcf114f4f2cde8298
Reviewed-by: Alex Blasche <alexander.blasche@digia.com>
Diffstat (limited to 'src/positioning/qgeorectangle.cpp')
-rw-r--r-- | src/positioning/qgeorectangle.cpp | 68 |
1 files changed, 67 insertions, 1 deletions
diff --git a/src/positioning/qgeorectangle.cpp b/src/positioning/qgeorectangle.cpp index 354bdb1c..3c2c0ff8 100644 --- a/src/positioning/qgeorectangle.cpp +++ b/src/positioning/qgeorectangle.cpp @@ -44,7 +44,7 @@ #include "qgeocoordinate.h" #include "qnumeric.h" - +#include <QList> QT_BEGIN_NAMESPACE /*! @@ -140,6 +140,24 @@ QGeoRectangle::QGeoRectangle(const QGeoCoordinate &topLeft, const QGeoCoordinate } /*! + Constructs a georectangle from the list of coordinates, the returned rectangle is the smallest possible + containing all the coordinates. + */ +QGeoRectangle::QGeoRectangle(const QList<QGeoCoordinate> &coordinates) +{ + if (coordinates.isEmpty()) { + d_ptr = new QGeoRectanglePrivate; + } else { + const QGeoCoordinate &startCoordinate = coordinates.first(); + d_ptr = new QGeoRectanglePrivate(startCoordinate, startCoordinate); + + foreach (const QGeoCoordinate &coordinate, coordinates) { + d_ptr->extendShape(coordinate); + } + } +} + +/*! Constructs a geo rectangle from the contents of \a other. */ QGeoRectangle::QGeoRectangle(const QGeoRectangle &other) @@ -735,6 +753,54 @@ QGeoRectangle QGeoRectangle::united(const QGeoRectangle &rectangle) const } /*! + Extends the rectangle in the smallest possible way to include \a coordinate in + the shape. + + Both the rectangle and coordinate needs to be valid. If the rectangle already covers + the coordinate noting happens. + +*/ +void QGeoRectanglePrivate::extendShape(const QGeoCoordinate &coordinate) +{ + if (!isValid() || !coordinate.isValid() || contains(coordinate)) + return; + + double left = topLeft.longitude(); + double right = bottomRight.longitude(); + double top = topLeft.latitude(); + double bottom = bottomRight.latitude(); + + double inputLat = coordinate.latitude(); + double inputLon = coordinate.longitude(); + + top = qMax(top, inputLat); + bottom = qMin(bottom, inputLat); + + bool wrap = left > right; + + if (wrap && inputLon > right && inputLon < left) { + if (qAbs(left - inputLon) < qAbs(right - inputLon)) + left = inputLon; + else + right = inputLon; + } else if (!wrap) { + if (inputLon < left) { + if (360 - (right - inputLon) < left - inputLon) + right = inputLon; + else + left = inputLon; + } else if (inputLon > right) { + if (360 - (inputLon - left) < inputLon - right) + left = inputLon; + else + right = inputLon; + } + } + topLeft = QGeoCoordinate(top, left); + bottomRight = QGeoCoordinate(bottom, right); +} + +/*! \fn QGeoRectangle QGeoRectangle::operator|(const QGeoRectangle &rectangle) const Returns the smallest geo rectangle which contains both this geo rectangle and \a rectangle. |