2 Situare - A location system for Facebook
3 Copyright (C) 2010 Ixonos Plc. Authors:
5 Sami Rämö - sami.ramo@ixonos.com
6 Jussi Laitinen - jussi.laitinen@ixonos.com
8 Situare is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 version 2 as published by the Free Software Foundation.
12 Situare is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Situare; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
25 #include <QStringList>
28 #include "mapengine.h"
30 //#include "mapscene.h"
35 MapEngine::MapEngine(MapView *mapView, QWidget *parent)
39 m_mapScene = new MapScene(this);
40 mapView->setScene(m_mapScene);
43 m_mapFetcher = new MapFetcher(new QNetworkAccessManager(this), this);
44 connect(m_mapFetcher, SIGNAL(mapImageReceived(QUrl,QPixmap)), this,
45 SLOT(mapImageReceived(QUrl, QPixmap)));
48 void MapEngine::setViewLocation(QPointF latLonCoordinate)
50 m_mapView->setZoomLevel(m_zoomLevel);
52 /// Fetch some map tiles for demo purposes
53 for (int x=9351; x<=9354; x++) {
54 for (int y=4261; y<=4264; y++) {
55 QString url = QString("http://tile.openstreetmap.org/mapnik/%1/%2/%3.png")
56 .arg(m_zoomLevel).arg(x).arg(y);
57 m_mapFetcher->fetchMapImage(QUrl(url));
62 void MapEngine::mapImageReceived(const QUrl &url, const QPixmap &pixmap)
65 QString path = url.path();
66 //qDebug() << __PRETTY_FUNCTION__ << "path:" << path;
68 QStringList pathParts = path.split("/", QString::SkipEmptyParts);
70 int zoom = (pathParts.at(1)).toInt();
71 int x = (pathParts.at(2)).toInt();
72 QString yString = pathParts.at(3);
74 int y = yString.toInt();
76 //qDebug() << __PRETTY_FUNCTION__ << zoom << x << y;
78 MapTile *mapTile = new MapTile();
79 mapTile->setZoomLevel(zoom);
80 mapTile->setTileNumber(QPoint(x, y));
81 mapTile->setPixmap(pixmap);
82 m_mapScene->addMapTile(mapTile);
85 QPoint MapEngine::latLonToTile(qreal latitude, qreal longitude, int zoom)
87 qDebug() << __PRETTY_FUNCTION__;
88 qreal z = static_cast<qreal>(1 << zoom);
90 qreal x = static_cast<qreal>((longitude + 180.0) / 360.0);
91 qreal y = static_cast<qreal>((1.0 - log(tan(latitude * M_PI / 180.0) + 1.0
92 / cos(latitude * M_PI / 180.0)) / M_PI) / 2.0);
94 return QPoint(qFloor(x*z), qFloor(y*z));
97 qreal MapEngine::tileXToLongitude(int x, int zoom)
99 qDebug() << __PRETTY_FUNCTION__;
100 qreal z = static_cast<qreal>(1 << zoom);
101 qreal lon = x / z * 360.0 - 180.0;
106 qreal MapEngine::tileYToLatitude(int y, int zoom)
108 qDebug() << __PRETTY_FUNCTION__;
109 qreal z = static_cast<qreal>(1 << zoom);
110 qreal n = M_PI - 2 * M_PI * y / zoom;
111 return 180.0 / (M_PI * atan(0.5 * exp(n) - exp(-n)));