SLOT(mapImageReceived(QUrl, QPixmap)));
}
-void MapEngine::setViewLocation(QPointF latLonCoordinate)
+void MapEngine::init()
{
emit zoomLevelChanged(m_zoomLevel);
+ setViewLocation(QPointF(25.5000, 65.0000));
- /// Fetch some map tiles for demo purposes
+ // Fetch some map tiles for demo purposes
for (int x=9351; x<=9354; x++) {
for (int y=4261; y<=4264; y++) {
QUrl url = buildURL(m_zoomLevel, QPoint(x, y));
}
}
+void MapEngine::setViewLocation(QPointF latLonCoordinate)
+{
+ setLocation(convertLatLonToSceneCoordinate(latLonCoordinate));
+}
+
QUrl MapEngine::buildURL(int zoomLevel, QPoint tileNumbers)
{
QString url = QString("http://tile.openstreetmap.org/mapnik/%1/%2/%3.png")
* @param parent Parent
*/
MapEngine(QObject *parent = 0);
+
+ /**
+ * @brief MapEngine initializer
+ *
+ * Set initial location and zoom level for the engine. locationChanged and
+ * zoomLevelChanged signals are emitted, so init should be called after
+ * those signals are connected to MapView.
+ */
+ void init();
/**
* @brief Convert tile x & y numbers to MapScene coordinates
QGraphicsScene* scene();
/**
- * @brief Set view location
+ * @brief Helper for setting view location based on latitude and longitude
+ * coordinates
*
* @param latLonCoordinate Latitude & longitude coordinates for location
*/
* @param latLonCoordinate latitude and longitude values
* @return QPoint tile x,y value
*/
- static QPoint convertLatLonToTile(int zoomLevel, QPointF latLonCoordinate)
+ static QPointF convertLatLonToSceneCoordinate(QPointF latLonCoordinate)
{
+ /// @todo CREATE TEST CASE & CHECK CALCULATION
qDebug() << __PRETTY_FUNCTION__;
qreal longitude = latLonCoordinate.x();
qreal latitude = latLonCoordinate.y();
- if ((zoomLevel > MAX_ZOOM_LEVEL) || (zoomLevel < MIN_ZOOM_LEVEL))
- return QPoint(UNDEFINED, UNDEFINED);
if ((longitude > MAX_LONGITUDE) || (longitude < MIN_LONGITUDE))
return QPoint(UNDEFINED, UNDEFINED);
if ((latitude > MAX_LATITUDE) || (latitude < MIN_LATITUDE))
return QPoint(UNDEFINED, UNDEFINED);
- qreal z = static_cast<qreal>(1 << zoomLevel);
+ qreal z = static_cast<qreal>(1 << MAX_ZOOM_LEVEL);
qreal x = static_cast<qreal>((longitude + 180.0) / 360.0);
qreal y = static_cast<qreal>((1.0 - log(tan(latitude * M_PI / 180.0) + 1.0
/ cos(latitude * M_PI / 180.0)) / M_PI) / 2.0);
- return QPoint(qFloor(x*z), qFloor(y*z));
+ return QPointF(x*z*TILE_SIZE_X, y*z*TILE_SIZE_Y);
}
private:
MapScene::MapScene(QObject *parent)
: QGraphicsScene(parent)
{
+ /// @todo Write test case for setting scene size
+ const int maxTilesPerSide = (1 << MAX_ZOOM_LEVEL);
+ const int maxPixelsPerSide = maxTilesPerSide * TILE_SIZE_X;
+ setSceneRect(0, 0, maxPixelsPerSide, maxPixelsPerSide);
}
void MapScene::addMapTile(MapTile *mapTile)
/**
* @brief Constructor
*
+ * Scene size is set to the amount of pixels on closest zoom level
* @param parent Parent
*/
MapScene(QObject *parent = 0);
(m_tileNumber.x() >= 0) && (m_tileNumber.x() <= maxTileNumber) &&
(m_tileNumber.y() >= 0) && (m_tileNumber.y() <= maxTileNumber)) {
setPos(MapEngine::convertTileNumberToSceneCoordinate(m_zoomLevel, m_tileNumber));
+ //qDebug() << __PRETTY_FUNCTION__ << "tile position:" << pos();
return; // done
}
// else
m_scenePosition += m_mousePosition - mapToScene(event->pos());
emit viewScrolled(m_scenePosition);
+ //qDebug() << __PRETTY_FUNCTION__ << "m_scenePosition" << m_scenePosition;
m_mousePosition = mapToScene(event->pos());
}
void MapView::centerToSceneCoordinates(QPointF sceneCoordinate)
{
+ //qDebug() << __PRETTY_FUNCTION__ << "sceneCoordinate" << sceneCoordinate;
centerOn(sceneCoordinate);
}
mapViewLayout->addWidget(mapView);
setLayout(mapViewLayout);
- mapEngine->setViewLocation(QPointF(25.5000, 65.0000));
+ mapEngine->init();
}