Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qgeocodingmanagerengineosm.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 Aaron McCarthy <mccarthy.aaron@gmail.com>
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5
6#include <QtCore/QVariantMap>
7#include <QtCore/QUrl>
8#include <QtCore/QUrlQuery>
9#include <QtCore/QLocale>
10#include <QtNetwork/QNetworkAccessManager>
11#include <QtNetwork/QNetworkRequest>
12#include <QtPositioning/QGeoCoordinate>
13#include <QtPositioning/QGeoAddress>
14#include <QtPositioning/QGeoShape>
15#include <QtPositioning/QGeoRectangle>
16#include "qgeocodereplyosm.h"
17
18
20
22{
23 return address.street() + QStringLiteral(", ") +
24 address.district() + QStringLiteral(", ") +
25 address.city() + QStringLiteral(", ") +
26 address.state() + QStringLiteral(", ") +
27 address.country();
28}
29
31{
32 return QString::number(rect.topLeft().longitude()) + QLatin1Char(',') +
33 QString::number(rect.topLeft().latitude()) + QLatin1Char(',') +
34 QString::number(rect.bottomRight().longitude()) + QLatin1Char(',') +
35 QString::number(rect.bottomRight().latitude());
36}
37
40 QString *errorString)
41: QGeoCodingManagerEngine(parameters), m_networkManager(new QNetworkAccessManager(this))
42{
43 if (parameters.contains(QStringLiteral("osm.useragent")))
44 m_userAgent = parameters.value(QStringLiteral("osm.useragent")).toString().toLatin1();
45 else
46 m_userAgent = "Qt Location based application";
47
48 if (parameters.contains(QStringLiteral("osm.geocoding.host")))
49 m_urlPrefix = parameters.value(QStringLiteral("osm.geocoding.host")).toString().toLatin1();
50 else
51 m_urlPrefix = QStringLiteral("https://nominatim.openstreetmap.org");
52
53 if (parameters.contains(QStringLiteral("osm.geocoding.debug_query")))
54 m_debugQuery = parameters.value(QStringLiteral("osm.geocoding.debug_query")).toBool();
55 if (parameters.contains(QStringLiteral("osm.geocoding.include_extended_data")))
56 m_includeExtraData = parameters.value(QStringLiteral("osm.geocoding.include_extended_data")).toBool();
57
59 errorString->clear();
60}
61
65
70
72{
74
76 request.setRawHeader("User-Agent", m_userAgent);
77
78 QUrl url(QString("%1/search").arg(m_urlPrefix));
81 query.addQueryItem(QStringLiteral("format"), QStringLiteral("json"));
82 query.addQueryItem(QStringLiteral("accept-language"), locale().name().left(2));
83 //query.addQueryItem(QStringLiteral("countrycodes"), QStringLiteral("au,jp"));
84 if (bounds.type() != QGeoShape::UnknownType) {
85 query.addQueryItem(QStringLiteral("viewbox"), boundingBoxToLtrb(bounds.boundingGeoRectangle()));
86 query.addQueryItem(QStringLiteral("bounded"), QStringLiteral("1"));
87 }
88 query.addQueryItem(QStringLiteral("polygon_geojson"), QStringLiteral("1"));
89 query.addQueryItem(QStringLiteral("addressdetails"), QStringLiteral("1"));
90 if (limit != -1)
91 query.addQueryItem(QStringLiteral("limit"), QString::number(limit));
92
95
96 QNetworkReply *reply = m_networkManager->get(request);
97
98 QGeoCodeReplyOsm *geocodeReply = new QGeoCodeReplyOsm(reply, m_includeExtraData, this);
99
100 connect(geocodeReply, &QGeoCodeReplyOsm::finished,
101 this, &QGeoCodingManagerEngineOsm::replyFinished);
103 this, &QGeoCodingManagerEngineOsm::replyError);
104
105 return geocodeReply;
106}
107
109 const QGeoShape &bounds)
110{
111 Q_UNUSED(bounds);
112
114 request.setRawHeader("User-Agent", m_userAgent);
115
116 QUrl url(QString("%1/reverse").arg(m_urlPrefix));
119 query.addQueryItem(QStringLiteral("accept-language"), locale().name().left(2));
120 query.addQueryItem(QStringLiteral("lat"), QString::number(coordinate.latitude()));
121 query.addQueryItem(QStringLiteral("lon"), QString::number(coordinate.longitude()));
122 query.addQueryItem(QStringLiteral("zoom"), QStringLiteral("18"));
123 query.addQueryItem(QStringLiteral("addressdetails"), QStringLiteral("1"));
124
127
128 QNetworkReply *reply = m_networkManager->get(request);
129
130 QGeoCodeReplyOsm *geocodeReply = new QGeoCodeReplyOsm(reply, m_includeExtraData, this);
131
132 connect(geocodeReply, &QGeoCodeReplyOsm::finished,
133 this, &QGeoCodingManagerEngineOsm::replyFinished);
135 this, &QGeoCodingManagerEngineOsm::replyError);
136
137 return geocodeReply;
138}
139
140void QGeoCodingManagerEngineOsm::replyFinished()
141{
142 QGeoCodeReply *reply = qobject_cast<QGeoCodeReply *>(sender());
143 if (reply)
145}
146
147void QGeoCodingManagerEngineOsm::replyError(QGeoCodeReply::Error errorCode, const QString &errorString)
148{
149 QGeoCodeReply *reply = qobject_cast<QGeoCodeReply *>(sender());
150 if (reply)
151 emit errorOccurred(reply, errorCode, errorString);
152}
153
\inmodule QtPositioning
Definition qgeoaddress.h:18
\inmodule QtLocation
void finished()
This signal is emitted when this reply has finished processing.
void errorOccurred(QGeoCodeReply::Error error, const QString &errorString=QString())
This signal is emitted when an error has been detected in the processing of this reply.
Error
Describes an error which prevented the completion of the operation.
QGeoCodeReply * reverseGeocode(const QGeoCoordinate &coordinate, const QGeoShape &bounds) override
Begins the reverse geocoding of coordinate.
QGeoCodingManagerEngineOsm(const QVariantMap &parameters, QGeoServiceProvider::Error *error, QString *errorString)
QGeoCodeReply * geocode(const QGeoAddress &address, const QGeoShape &bounds) override
Begins the geocoding of address.
void errorOccurred(QGeoCodeReply *reply, QGeoCodeReply::Error error, const QString &errorString=QString())
void finished(QGeoCodeReply *reply)
QLocale locale() const
Returns the locale used to hint to this geocoding manager about what language to use for the results.
\inmodule QtPositioning
double longitude
This property holds the longitude in decimal degrees.
double latitude
This property holds the latitude in decimal degrees.
\inmodule QtPositioning
Error
Describes an error related to the loading and setup of a service provider plugin.
\inmodule QtPositioning
Definition qgeoshape.h:17
ShapeType type
This property holds the type of this geo shape.
Definition qgeoshape.h:19
Q_INVOKABLE QGeoRectangle boundingGeoRectangle() const
Returns a QGeoRectangle representing the geographical bounding rectangle of the geo shape,...
@ UnknownType
Definition qgeoshape.h:31
T value(const Key &key, const T &defaultValue=T()) const
Definition qmap.h:357
bool contains(const Key &key) const
Definition qmap.h:341
The QNetworkAccessManager class allows the application to send network requests and receive replies.
QNetworkReply * get(const QNetworkRequest &request)
Posts a request to obtain the contents of the target request and returns a new QNetworkReply object o...
The QNetworkReply class contains the data and headers for a request sent with QNetworkAccessManager.
The QNetworkRequest class holds a request to be sent with QNetworkAccessManager.
void setRawHeader(const QByteArray &headerName, const QByteArray &value)
Sets the header headerName to be of value headerValue.
void setUrl(const QUrl &url)
Sets the URL this network request is referring to be url.
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
QObject * sender() const
Returns a pointer to the object that sent the signal, if called in a slot activated by a signal; othe...
Definition qobject.cpp:2658
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
QByteArray toLatin1() const &
Definition qstring.h:630
static QString number(int, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:8084
\inmodule QtCore
Definition qurlquery.h:20
void addQueryItem(const QString &key, const QString &value)
Appends the pair key = value to the end of the query string of the URL.
\inmodule QtCore
Definition qurl.h:94
void setQuery(const QString &query, ParsingMode mode=TolerantMode)
Sets the query string of the URL to query.
Definition qurl.cpp:2550
QString toString() const
Returns the variant as a QString if the variant has a userType() including, but not limited to:
bool toBool() const
Returns the variant as a bool if the variant has userType() Bool.
#define this
Definition dialogs.cpp:9
rect
[4]
static QString boundingBoxToLtrb(const QGeoRectangle &rect)
static QString addressToQuery(const QGeoAddress &address)
Combined button and popup list for selecting options.
DBusConnection const char DBusError * error
static QString boundingBoxToLtrb(const QGeoRectangle &rect)
static QT_BEGIN_NAMESPACE QString addressToQuery(const QGeoAddress &address)
GLint left
GLenum GLuint GLintptr offset
GLuint name
GLenum query
GLint limit
GLuint GLuint64EXT address
SSL_CTX int void * arg
#define QStringLiteral(str)
#define emit
#define Q_UNUSED(x)
QUrl url("example.com")
[constructor-url-reference]
QNetworkRequest request(url)
QNetworkReply * reply
\inmodule QtCore \reentrant
Definition qchar.h:18