-
Notifications
You must be signed in to change notification settings - Fork 0
/
qgeocodingmanagerenginegooglemaps.cpp
132 lines (106 loc) · 4.87 KB
/
qgeocodingmanagerenginegooglemaps.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
#include "qgeocodingmanagerenginegooglemaps.h"
#include "qgeocodereplygooglemaps.h"
#include <QtCore/QVariantMap>
#include <QtCore/QUrl>
#include <QtCore/QUrlQuery>
#include <QtCore/QLocale>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkRequest>
#include <QtPositioning/QGeoCoordinate>
#include <QtPositioning/QGeoAddress>
#include <QtPositioning/QGeoShape>
#include <QtPositioning/QGeoRectangle>
static QString addressToQuery(const QGeoAddress &address)
{
return address.street() + QStringLiteral(",+") +
address.district() + QStringLiteral(",+") +
address.city() + QStringLiteral(",+") +
address.state() + QStringLiteral(",+") +
address.country();
}
static QString coordinateToQuery(const QGeoCoordinate &coordinate)
{
return QString::number(coordinate.latitude()) + QStringLiteral(",") +
QString::number(coordinate.longitude());
}
QGeoCodingManagerEngineGooglemaps::QGeoCodingManagerEngineGooglemaps(const QVariantMap ¶meters,
QGeoServiceProvider::Error *error,
QString *errorString)
: QGeoCodingManagerEngine(parameters), m_networkManager(new QNetworkAccessManager(this))
{
if (parameters.contains(QStringLiteral("googlemaps.useragent")))
m_userAgent = parameters.value(QStringLiteral("googlemaps.useragent")).toString().toLatin1();
else
m_userAgent = "Qt Location based application";
if(parameters.contains((QStringLiteral("googlemaps.geocode.apikey"))))
m_apiKey = parameters.value(QStringLiteral("googlemaps.geocode.apikey")).toString();
else
m_apiKey = parameters.value(QStringLiteral("googlemaps.apikey")).toString();
m_urlPrefix = QStringLiteral("https://maps.googleapis.com/maps/api/geocode/json");
*error = QGeoServiceProvider::NoError;
errorString->clear();
}
QGeoCodingManagerEngineGooglemaps::~QGeoCodingManagerEngineGooglemaps()
{
}
QGeoCodeReply *QGeoCodingManagerEngineGooglemaps::geocode(const QGeoAddress &address, const QGeoShape &bounds)
{
return geocode(addressToQuery(address), -1, -1, bounds);
}
QGeoCodeReply *QGeoCodingManagerEngineGooglemaps::geocode(const QString &address, int limit, int offset, const QGeoShape &bounds)
{
Q_UNUSED(offset)
Q_UNUSED(limit)
QNetworkRequest request;
request.setRawHeader("User-Agent", m_userAgent);
QUrl url(m_urlPrefix);
QUrlQuery query;
query.addQueryItem(QStringLiteral("address"), address);
query.addQueryItem(QStringLiteral("key"), m_apiKey);
if (bounds.isValid() && !bounds.isEmpty() && bounds.type() != QGeoShape::UnknownType) {
if (bounds.type() == QGeoShape::RectangleType) {
const QGeoRectangle &r = static_cast<const QGeoRectangle&>(bounds);
query.addQueryItem(QStringLiteral("bounds"),
(coordinateToQuery(r.topRight()) + "|" + coordinateToQuery(r.bottomLeft())));
}
}
url.setQuery(query);
request.setUrl(url);
QNetworkReply *reply = m_networkManager->get(request);
QGeoCodeReplyGooglemaps *geocodeReply = new QGeoCodeReplyGooglemaps(reply, this);
connect(geocodeReply, SIGNAL(finished()), this, SLOT(replyFinished()));
connect(geocodeReply, SIGNAL(error(QGeoCodeReply::Error,QString)),
this, SLOT(replyError(QGeoCodeReply::Error,QString)));
return geocodeReply;
}
QGeoCodeReply *QGeoCodingManagerEngineGooglemaps::reverseGeocode(const QGeoCoordinate &coordinate,
const QGeoShape &bounds)
{
Q_UNUSED(bounds)
QNetworkRequest request;
request.setRawHeader("User-Agent", m_userAgent);
QUrl url(m_urlPrefix);
QUrlQuery query;
query.addQueryItem(QStringLiteral("latlng"), coordinateToQuery(coordinate));
query.addQueryItem(QStringLiteral("key"), m_apiKey);
url.setQuery(query);
request.setUrl(url);
QNetworkReply *reply = m_networkManager->get(request);
QGeoCodeReplyGooglemaps *geocodeReply = new QGeoCodeReplyGooglemaps(reply, this);
connect(geocodeReply, SIGNAL(finished()), this, SLOT(replyFinished()));
connect(geocodeReply, SIGNAL(error(QGeoCodeReply::Error,QString)),
this, SLOT(replyError(QGeoCodeReply::Error,QString)));
return geocodeReply;
}
void QGeoCodingManagerEngineGooglemaps::replyFinished()
{
QGeoCodeReply *reply = qobject_cast<QGeoCodeReply *>(sender());
if (reply)
emit finished(reply);
}
void QGeoCodingManagerEngineGooglemaps::replyError(QGeoCodeReply::Error errorCode, const QString &errorString)
{
QGeoCodeReply *reply = qobject_cast<QGeoCodeReply *>(sender());
if (reply)
emit error(reply, errorCode, errorString);
}