Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qgeocodereplymapbox.cpp
Go to the documentation of this file.
1// Copyright (C) 2017 Mapbox, Inc.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
6
7#include <QtCore/QJsonDocument>
8#include <QtCore/QJsonObject>
9#include <QtCore/QJsonArray>
10#include <QtPositioning/QGeoCoordinate>
11#include <QtPositioning/QGeoAddress>
12#include <QtPositioning/QGeoLocation>
13#include <QtPositioning/QGeoRectangle>
14
16
17QGeoCodeReplyMapbox::QGeoCodeReplyMapbox(QNetworkReply *reply, QObject *parent)
19{
20 Q_ASSERT(parent);
21 if (!reply) {
22 setError(UnknownError, QStringLiteral("Null reply"));
23 return;
24 }
25
26 connect(reply, &QNetworkReply::finished, this, &QGeoCodeReplyMapbox::onNetworkReplyFinished);
27 connect(reply, &QNetworkReply::errorOccurred, this, &QGeoCodeReplyMapbox::onNetworkReplyError);
28
29 connect(this, &QGeoCodeReply::aborted, reply, &QNetworkReply::abort);
30 connect(this, &QObject::destroyed, reply, &QObject::deleteLater);
31}
32
33QGeoCodeReplyMapbox::~QGeoCodeReplyMapbox()
34{
35}
36
37void QGeoCodeReplyMapbox::onNetworkReplyFinished()
38{
39 QNetworkReply *reply = static_cast<QNetworkReply *>(sender());
40 reply->deleteLater();
41
42 if (reply->error() != QNetworkReply::NoError)
43 return;
44
45 QList<QGeoLocation> locations;
46 QJsonDocument document = QJsonDocument::fromJson(reply->readAll());
47 if (!document.isObject()) {
48 setError(ParseError, tr("Response parse error"));
49 return;
50 }
51
52 const QJsonArray features = document.object().value(QStringLiteral("features")).toArray();
53 for (const QJsonValueConstRef value : features)
54 locations.append(QMapboxCommon::parseGeoLocation(value.toObject()));
55
56 setLocations(locations);
57
58 setFinished(true);
59}
60
61void QGeoCodeReplyMapbox::onNetworkReplyError(QNetworkReply::NetworkError error)
62{
63 Q_UNUSED(error);
64 QNetworkReply *reply = static_cast<QNetworkReply *>(sender());
65 reply->deleteLater();
66 setError(QGeoCodeReply::CommunicationError, reply->errorString());
67}
68
69QT_END_NAMESPACE
The QNetworkReply class contains the data and headers for a request sent with QNetworkAccessManager.
QObject * parent
Definition qobject.h:73
Combined button and popup list for selecting options.
connect(quitButton, &QPushButton::clicked, &app, &QCoreApplication::quit, Qt::QueuedConnection)