-
Notifications
You must be signed in to change notification settings - Fork 59
/
qgeotilefetchergooglemaps.cpp
212 lines (184 loc) · 7.36 KB
/
qgeotilefetchergooglemaps.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include "qgeotilefetchergooglemaps.h"
#include "qgeomapreplygooglemaps.h"
#include "qgeotiledmapgooglemaps.h"
#include "qgeotiledmappingmanagerenginegooglemaps.h"
#include <QtLocation/private/qgeotilespec_p.h>
#include <QDebug>
#include <QSize>
#include <QDir>
#include <QUrl>
#include <QUrlQuery>
#include <QTime>
#include <QNetworkProxy>
#include <QtCore/QJsonDocument>
#include <QSslSocket>
#include <math.h>
#include <map>
QT_BEGIN_NAMESPACE
QGeoTileFetcherGooglemaps::QGeoTileFetcherGooglemaps(const QVariantMap ¶meters,
QGeoTiledMappingManagerEngineGooglemaps *engine,
const QSize &tileSize)
: QGeoTileFetcher(engine),
m_networkManager(new QNetworkAccessManager(this)),
m_engineGooglemaps(engine),
m_tileSize(tileSize),
_googleVersionRetrieved(false),
_scale(1)
{
if(parameters.contains(QStringLiteral("googlemaps.maps.apikey")))
m_apiKey = parameters.value(QStringLiteral("googlemaps.maps.apikey")).toString();
else
m_apiKey = parameters.value(QStringLiteral("googlemaps.apikey")).toString();
m_signature = parameters.value(QStringLiteral("googlemaps.maps.signature")).toString();
m_client = parameters.value(QStringLiteral("googlemaps.maps.client")).toString();
m_baseUri = QStringLiteral("https://maps.googleapis.com/maps/api/staticmap");
if (parameters.contains(QStringLiteral("googlemaps.useragent")))
_userAgent = parameters.value(QStringLiteral("googlemaps.useragent")).toString().toLatin1();
else
_userAgent = "";
if (parameters.contains(QStringLiteral("googlemaps.maps.language"))) {
_language = parameters.value(QStringLiteral("googlemaps.maps.language")).toString().toLatin1();
if (_language.isEmpty())
_language = "en-US";
} else {
QStringList langs = QLocale::system().uiLanguages();
_language = (langs.length() > 0) ? langs[0] : "en-US";
}
if (parameters.contains(QStringLiteral("googlemaps.maps.highdpi")))
_scale = (parameters.value(QStringLiteral("googlemaps.maps.highdpi")).toBool()) ? 2 : 1;
// Google version strings
_secGoogleWord = "Galileo";
}
QGeoTileFetcherGooglemaps::~QGeoTileFetcherGooglemaps()
{
}
void QGeoTileFetcherGooglemaps::_getSessionToken()
{
QUrl sessionUrl("https://www.googleapis.com/tile/v1/createSession");
QUrlQuery queryItems;
queryItems.addQueryItem("key", m_apiKey);
queryItems.addQueryItem("mapType", "roadmap");
queryItems.addQueryItem("language", _language);
queryItems.addQueryItem("region", "de");
sessionUrl.setQuery(queryItems);
netRequest.setUrl(sessionUrl);
QNetworkReply *sessionReply = m_networkManager->get(netRequest);
if (sessionReply->error() != QNetworkReply::NoError)
return;
QJsonDocument document = QJsonDocument::fromJson(sessionReply->readAll());
if (!document.isObject())
return;
QJsonObject object = document.object();
QJsonValue status = object.value(QStringLiteral("session"));
printf("%s", status.toString().toLatin1().data());
}
QGeoTiledMapReply *QGeoTileFetcherGooglemaps::getTileImage(const QGeoTileSpec &spec)
{
QString surl = _getURL(spec.mapId(), spec.x(), spec.y(), spec.zoom());
QUrl url(surl);
netRequest.setUrl(url);
QNetworkReply *netReply = m_networkManager->get(netRequest);
QGeoTiledMapReply *mapReply = new QGeoMapReplyGooglemaps(netReply, spec);
return mapReply;
}
void QGeoTileFetcherGooglemaps::_getSecGoogleWords(int x, int y, QString &sec1, QString &sec2)
{
sec1 = ""; // after &x=...
sec2 = ""; // after &zoom=...
int seclen = ((x * 3) + y) % 8;
sec2 = _secGoogleWord.left(seclen);
if (y >= 10000 && y < 100000) {
sec1 = "&s=";
}
}
QString QGeoTileFetcherGooglemaps::_getURL(int type, int x, int y, int zoom)
{
switch (type) {
case 0:
case 1: //Road Map
{
QString sec1 = ""; // after &x=...
QString sec2 = ""; // after &zoom=...
_getSecGoogleWords(x, y, sec1, sec2);
return QString("https://mt.google.com/vt/lyrs=m&hl=%1&x=%2%3&y=%4&z=%5&s=%6&scale=%7").arg(_language).arg(x).arg(sec1).arg(y).arg(zoom).arg(sec2).arg(_scale);
}
break;
case 2: //Satallite Map
{
QString sec1 = ""; // after &x=...
QString sec2 = ""; // after &zoom=...
_getSecGoogleWords(x, y, sec1, sec2);
return QString("https://mt.google.com/vt/lyrs=s&hl=%1&x=%2%3&y=%4&z=%5&s=%6&scale=%7").arg(_language).arg(x).arg(sec1).arg(y).arg(zoom).arg(sec2).arg(_scale);
}
break;
case 3: //Terrain Map
{
QString sec1 = ""; // after &x=...
QString sec2 = ""; // after &zoom=...
_getSecGoogleWords(x, y, sec1, sec2);
return QString("https://mt.google.com/vt/lyrs=p&hl=%1&x=%2%3&y=%4&z=%5&s=%6&scale=%7").arg(_language).arg(x).arg(sec1).arg(y).arg(zoom).arg(sec2).arg(_scale);
}
break;
case 4: //Hybrid Map
{
QString sec1 = ""; // after &x=...
QString sec2 = ""; // after &zoom=...
_getSecGoogleWords(x, y, sec1, sec2);
return QString("https://mt.google.com/vt/lyrs=y&hl=%1&x=%2%3&y=%4&z=%5&s=%6&scale=%7").arg(_language).arg(x).arg(sec1).arg(y).arg(zoom).arg(sec2).arg(_scale);
}
break;
}
return "";
}
void QGeoTileFetcherGooglemaps::_networkReplyError(QNetworkReply::NetworkError error)
{
qWarning() << "Could not connect to google maps. Error:" << error;
if(_googleReply)
{
_googleReply->deleteLater();
_googleReply = NULL;
}
}
void QGeoTileFetcherGooglemaps::_replyDestroyed()
{
_googleReply = NULL;
}
void QGeoTileFetcherGooglemaps::_googleVersionCompleted()
{
if (!_googleReply || (_googleReply->error() != QNetworkReply::NoError)) {
qDebug() << "Error collecting Google maps version info";
return;
}
_googleReply->deleteLater();
_googleReply = NULL;
}
void QGeoTileFetcherGooglemaps::_tryCorrectGoogleVersions(QNetworkAccessManager* networkManager)
{
QMutexLocker locker(&_googleVersionMutex);
if (_googleVersionRetrieved) {
return;
}
_googleVersionRetrieved = true;
if(networkManager)
{
QNetworkRequest qheader;
QNetworkProxy proxy = networkManager->proxy();
QNetworkProxy tProxy;
tProxy.setType(QNetworkProxy::DefaultProxy);
networkManager->setProxy(tProxy);
#ifndef QT_NO_SSL
QSslConfiguration conf = qheader.sslConfiguration();
conf.setPeerVerifyMode(QSslSocket::VerifyNone);
qheader.setSslConfiguration(conf);
#endif
QString url = "https://maps.google.com/maps/api/js?v=3.2&sensor=false";
qheader.setUrl(QUrl(url));
qheader.setRawHeader("User-Agent", _userAgent);
_googleReply = networkManager->get(qheader);
connect(_googleReply, &QNetworkReply::finished, this, &QGeoTileFetcherGooglemaps::_googleVersionCompleted);
connect(_googleReply, &QNetworkReply::destroyed, this, &QGeoTileFetcherGooglemaps::_replyDestroyed);
connect(_googleReply, &QNetworkReply::errorOccurred, this, &QGeoTileFetcherGooglemaps::_networkReplyError);
networkManager->setProxy(proxy);
}
}
QT_END_NAMESPACE