|
1 |
| -#include "qgeocodereplygooglemaps.h" |
2 |
| - |
3 |
| - |
4 |
| -#include <QtPositioning/QGeoCoordinate> |
5 |
| -#include <QtPositioning/QGeoAddress> |
6 |
| -#include <QtPositioning/QGeoLocation> |
7 |
| -#include <QtPositioning/QGeoRectangle> |
8 |
| - |
9 |
| -QGeoCodeReplyGooglemaps::QGeoCodeReplyGooglemaps(QNetworkReply *reply, QObject *parent) |
10 |
| -: QGeoCodeReply(parent), m_reply(reply) |
11 |
| -{ |
12 |
| - connect(m_reply, SIGNAL(finished()), this, SLOT(networkReplyFinished())); |
13 |
| - connect(m_reply, SIGNAL(error(QNetworkReply::NetworkError)), |
14 |
| - this, SLOT(networkReplyError(QNetworkReply::NetworkError))); |
15 |
| - |
16 |
| - setLimit(1); |
17 |
| - setOffset(0); |
18 |
| -} |
19 |
| - |
20 |
| -QGeoCodeReplyGooglemaps::~QGeoCodeReplyGooglemaps() |
21 |
| -{ |
22 |
| - if (m_reply) |
23 |
| - m_reply->deleteLater(); |
24 |
| -} |
25 |
| - |
26 |
| -void QGeoCodeReplyGooglemaps::abort() |
27 |
| -{ |
28 |
| - if (!m_reply) |
29 |
| - return; |
30 |
| - |
31 |
| - m_reply->abort(); |
32 |
| - |
33 |
| - m_reply->deleteLater(); |
34 |
| - m_reply = 0; |
35 |
| -} |
36 |
| - |
37 |
| - |
38 |
| -void QGeoCodeReplyGooglemaps::networkReplyFinished() |
39 |
| -{ |
40 |
| - if (!m_reply) |
41 |
| - return; |
42 |
| - |
43 |
| - if (m_reply->error() != QNetworkReply::NoError) |
44 |
| - return; |
45 |
| - |
46 |
| - QList<QGeoLocation> locations; |
47 |
| -// setError(QGeoCodeReply::ParseError, QStringLiteral("Error parsing OpenRouteService xml response:") + xml.errorString() + " at line: " + xml.lineNumber()); |
48 |
| - setLocations(locations); |
49 |
| - setFinished(true); |
50 |
| - |
51 |
| - m_reply->deleteLater(); |
52 |
| - m_reply = 0; |
53 |
| -} |
54 |
| - |
55 |
| -void QGeoCodeReplyGooglemaps::networkReplyError(QNetworkReply::NetworkError error) |
56 |
| -{ |
57 |
| - Q_UNUSED(error) |
58 |
| - |
59 |
| - if (!m_reply) |
60 |
| - return; |
61 |
| - |
62 |
| - setError(QGeoCodeReply::CommunicationError, m_reply->errorString()); |
63 |
| - |
64 |
| - m_reply->deleteLater(); |
65 |
| - m_reply = 0; |
66 |
| -} |
| 1 | +#include "qgeocodereplygooglemaps.h" |
| 2 | + |
| 3 | + |
| 4 | +#include <QtPositioning/QGeoCoordinate> |
| 5 | +#include <QtPositioning/QGeoAddress> |
| 6 | +#include <QtPositioning/QGeoLocation> |
| 7 | +#include <QtPositioning/QGeoRectangle> |
| 8 | + |
| 9 | +#include <QtCore/QJsonDocument> |
| 10 | +#include <QtCore/QJsonArray> |
| 11 | +#include <QtCore/QJsonObject> |
| 12 | + |
| 13 | +static QGeoCoordinate constructCoordiante(const QJsonObject &jsonCoord) { |
| 14 | + QGeoCoordinate coord(0,0); |
| 15 | + coord.setLatitude(jsonCoord.value(QStringLiteral("lat")).toDouble()); |
| 16 | + coord.setLongitude(jsonCoord.value(QStringLiteral("lng")).toDouble()); |
| 17 | + return coord; |
| 18 | +} |
| 19 | + |
| 20 | +static bool checkAddressType(const QJsonObject &jsonAddress, const QString &type) { |
| 21 | + QJsonArray a = jsonAddress.value("types").toArray(); |
| 22 | + for (int i = 0; i < a.size(); i++) { |
| 23 | + if (a.at(i).toString() == type) |
| 24 | + return true; |
| 25 | + } |
| 26 | + return false; |
| 27 | +} |
| 28 | + |
| 29 | +QGeoCodeReplyGooglemaps::QGeoCodeReplyGooglemaps(QNetworkReply *reply, QObject *parent) |
| 30 | + : QGeoCodeReply(parent), m_reply(reply) |
| 31 | +{ |
| 32 | + connect(m_reply, SIGNAL(finished()), this, SLOT(networkReplyFinished())); |
| 33 | + connect(m_reply, SIGNAL(error(QNetworkReply::NetworkError)), |
| 34 | + this, SLOT(networkReplyError(QNetworkReply::NetworkError))); |
| 35 | + |
| 36 | + setLimit(1); |
| 37 | + setOffset(0); |
| 38 | +} |
| 39 | + |
| 40 | +QGeoCodeReplyGooglemaps::~QGeoCodeReplyGooglemaps() |
| 41 | +{ |
| 42 | + if (m_reply) |
| 43 | + m_reply->deleteLater(); |
| 44 | +} |
| 45 | + |
| 46 | +void QGeoCodeReplyGooglemaps::abort() |
| 47 | +{ |
| 48 | + if (!m_reply) |
| 49 | + return; |
| 50 | + |
| 51 | + m_reply->abort(); |
| 52 | + |
| 53 | + m_reply->deleteLater(); |
| 54 | + m_reply = 0; |
| 55 | +} |
| 56 | + |
| 57 | + |
| 58 | +void QGeoCodeReplyGooglemaps::networkReplyFinished() |
| 59 | +{ |
| 60 | + if (!m_reply) |
| 61 | + return; |
| 62 | + |
| 63 | + if (m_reply->error() != QNetworkReply::NoError) |
| 64 | + return; |
| 65 | + |
| 66 | + QString status; |
| 67 | + |
| 68 | + QList<QGeoLocation> locations; |
| 69 | + // setError(QGeoCodeReply::ParseError, QStringLiteral("Error parsing OpenRouteService xml response:") + xml.errorString() + " at line: " + xml.lineNumber()); |
| 70 | + QJsonDocument document = QJsonDocument::fromJson(m_reply->read(m_reply->bytesAvailable())); |
| 71 | + if (document.isObject()) { |
| 72 | + QJsonObject object = document.object(); |
| 73 | + |
| 74 | + status = object.value(QStringLiteral("status")).toString(); |
| 75 | + if (status == "OK") { |
| 76 | + QJsonArray jsonlocations = object.value(QStringLiteral("results")).toArray(); |
| 77 | + qDebug() << "locations:" << jsonlocations.size(); |
| 78 | + for(int i = 0; i < jsonlocations.size(); i++) { |
| 79 | + QGeoLocation location; |
| 80 | + QGeoAddress address; |
| 81 | + |
| 82 | + QJsonObject o = jsonlocations.at(i).toObject(); |
| 83 | + address.setText(o.value("formatted_address").toString()); |
| 84 | + QJsonObject ogeometry = o.value("geometry").toObject(); |
| 85 | + |
| 86 | + location.setCoordinate(constructCoordiante(ogeometry.value("location").toObject())); |
| 87 | + |
| 88 | + QJsonObject jaddressRanges = ogeometry.value("viewport").toObject(); |
| 89 | + if (!jaddressRanges.isEmpty()) { |
| 90 | + QGeoRectangle r; |
| 91 | + r.setTopRight(constructCoordiante(jaddressRanges.value("northeast").toObject())); |
| 92 | + r.setBottomLeft(constructCoordiante(jaddressRanges.value("southwest").toObject())); |
| 93 | + location.setBoundingBox(r); |
| 94 | + } |
| 95 | + |
| 96 | + QJsonArray jaddress = o.value("address_components").toArray(); |
| 97 | + |
| 98 | + QString street_name; |
| 99 | + QString street_num; |
| 100 | + for(int j = 0; j < jaddress.size(); j++) { |
| 101 | + QJsonObject addobj = jaddress.at(j).toObject(); |
| 102 | + if (checkAddressType(addobj, "street_number")) |
| 103 | + street_num = addobj.value("long_name").toString(); |
| 104 | + if (checkAddressType(addobj, "route")) |
| 105 | + street_name = addobj.value("long_name").toString(); |
| 106 | + if (checkAddressType(addobj, "country")) |
| 107 | + address.setCountryCode(addobj.value("short_name").toString()); |
| 108 | + if (checkAddressType(addobj, "administrative_area_level_3")) |
| 109 | + address.setCity(addobj.value("long_name").toString()); |
| 110 | + if (checkAddressType(addobj, "administrative_area_level_2")) |
| 111 | + address.setCounty(addobj.value("long_name").toString()); |
| 112 | + if (checkAddressType(addobj, "administrative_area_level_1")) |
| 113 | + address.setState(addobj.value("long_name").toString()); |
| 114 | + if (checkAddressType(addobj, "sublocality")) |
| 115 | + address.setDistrict(addobj.value("long_name").toString()); |
| 116 | + if (checkAddressType(addobj, "postal_code")) |
| 117 | + address.setPostalCode(addobj.value("long_name").toString()); |
| 118 | + } |
| 119 | + address.setStreet(street_name + (street_num.size() > 0 ? ( ", " + street_num) : "")); |
| 120 | + |
| 121 | + location.setAddress(address); |
| 122 | + |
| 123 | + locations.append(location); |
| 124 | + } |
| 125 | + } |
| 126 | + } else { |
| 127 | + if (status == "ZERO_RESULTS") |
| 128 | + setError(CombinationError, "Geocode was successful but returned no results. This may occur if the geocoder was passed a non-existent address"); |
| 129 | + else if (status == "OVER_QUERY_LIMIT") |
| 130 | + setError(CommunicationError, "Request quota is over"); |
| 131 | + else if (status == "REQUEST_DENIED") |
| 132 | + setError(CommunicationError, "Request denied"); |
| 133 | + else if (status == "INVALID_REQUEST") |
| 134 | + setError(UnsupportedOptionError, "Address, components or latlng is missing"); |
| 135 | + else if (status == "UNKNOWN_ERROR") |
| 136 | + setError(UnknownError, "Request could not be processed due to a server error. Try again later"); |
| 137 | + } |
| 138 | + |
| 139 | + setLocations(locations); |
| 140 | + setFinished(true); |
| 141 | + |
| 142 | + m_reply->deleteLater(); |
| 143 | + m_reply = 0; |
| 144 | +} |
| 145 | + |
| 146 | +void QGeoCodeReplyGooglemaps::networkReplyError(QNetworkReply::NetworkError error) |
| 147 | +{ |
| 148 | + Q_UNUSED(error) |
| 149 | + |
| 150 | + if (!m_reply) |
| 151 | + return; |
| 152 | + |
| 153 | + setError(QGeoCodeReply::CommunicationError, m_reply->errorString()); |
| 154 | + |
| 155 | + m_reply->deleteLater(); |
| 156 | + m_reply = 0; |
| 157 | +} |
0 commit comments