forked from SindenDev/amap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qgeocodingmanagerengineamap.cpp
132 lines (106 loc) · 4.62 KB
/
qgeocodingmanagerengineamap.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 "qgeocodingmanagerengineamap.h"
#include "qgeocodereplyamap.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());
}
QGeoCodingManagerEngineAmap::QGeoCodingManagerEngineAmap(const QVariantMap ¶meters,
QGeoServiceProvider::Error *error,
QString *errorString)
: QGeoCodingManagerEngine(parameters), m_networkManager(new QNetworkAccessManager(this))
{
if (parameters.contains(QStringLiteral("amap.useragent")))
m_userAgent = parameters.value(QStringLiteral("amap.useragent")).toString().toLatin1();
else
m_userAgent = "Qt Location based application";
if(parameters.contains((QStringLiteral("amap.geocode.apikey"))))
m_apiKey = parameters.value(QStringLiteral("amap.geocode.apikey")).toString();
else
m_apiKey = parameters.value(QStringLiteral("amap.apikey")).toString();
m_urlPrefix = QStringLiteral("https://maps.amap.com/maps/api/geocode/json");
*error = QGeoServiceProvider::NoError;
errorString->clear();
}
QGeoCodingManagerEngineAmap::~QGeoCodingManagerEngineAmap()
{
}
QGeoCodeReply *QGeoCodingManagerEngineAmap::geocode(const QGeoAddress &address, const QGeoShape &bounds)
{
return geocode(addressToQuery(address), -1, -1, bounds);
}
QGeoCodeReply *QGeoCodingManagerEngineAmap::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);
QGeoCodeReplyAmap *geocodeReply = new QGeoCodeReplyAmap(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 *QGeoCodingManagerEngineAmap::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);
QGeoCodeReplyAmap *geocodeReply = new QGeoCodeReplyAmap(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 QGeoCodingManagerEngineAmap::replyFinished()
{
QGeoCodeReply *reply = qobject_cast<QGeoCodeReply *>(sender());
if (reply)
emit finished(reply);
}
void QGeoCodingManagerEngineAmap::replyError(QGeoCodeReply::Error errorCode, const QString &errorString)
{
QGeoCodeReply *reply = qobject_cast<QGeoCodeReply *>(sender());
if (reply)
emit error(reply, errorCode, errorString);
}