add Map inverse geocoding
add minimum/maximum to ScrollSpinBox
This commit is contained in:
92
libs/map/osm_geocoding.cpp
Normal file
92
libs/map/osm_geocoding.cpp
Normal file
@@ -0,0 +1,92 @@
|
||||
#include "osm_geocoding.h"
|
||||
|
||||
#include <QJsonObject>
|
||||
#include <QJsonValue>
|
||||
#include <QLocale>
|
||||
#include <QNetworkAccessManager>
|
||||
#include <QNetworkReply>
|
||||
#include <QNetworkRequest>
|
||||
|
||||
|
||||
OSMGeocodingResult::OSMGeocodingResult(QObject * parent): QObject(parent) {}
|
||||
|
||||
|
||||
OSMGeocodingResult::~OSMGeocodingResult() {}
|
||||
|
||||
|
||||
void OSMGeocodingResult::abort() {
|
||||
if (reply) {
|
||||
reply->abort();
|
||||
reply->deleteLater();
|
||||
blockSignals(true);
|
||||
deleteLater();
|
||||
reply = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
QString OSMGeocodingResult::displayName() const {
|
||||
return res.object().value("display_name").toString();
|
||||
}
|
||||
|
||||
|
||||
QPointF OSMGeocodingResult::resultCoordinate() const {
|
||||
auto loc_c = QLocale::c();
|
||||
return QPointF(loc_c.toDouble(res.object().value("lat").toString()), loc_c.toDouble(res.object().value("lng").toString()));
|
||||
}
|
||||
|
||||
|
||||
void OSMGeocodingResult::requestDone() {
|
||||
emit ready();
|
||||
deleteLater();
|
||||
}
|
||||
|
||||
|
||||
// OSMGeocoding
|
||||
|
||||
OSMGeocoding::OSMGeocoding(): QObject() {
|
||||
nam = new QNetworkAccessManager();
|
||||
provider = "https://nominatim.openstreetmap.org";
|
||||
// https://nominatim.openstreetmap.org/reverse?lat=52.79870&lon=27.56572&format=json
|
||||
}
|
||||
|
||||
|
||||
OSMGeocoding::~OSMGeocoding() {
|
||||
delete nam;
|
||||
}
|
||||
|
||||
|
||||
OSMGeocoding * OSMGeocoding::instance() {
|
||||
static OSMGeocoding ret;
|
||||
return &ret;
|
||||
}
|
||||
|
||||
|
||||
OSMGeocodingResult * OSMGeocoding::queue(QPointF coord) {
|
||||
return instance()->reverseCoding(coord);
|
||||
}
|
||||
|
||||
|
||||
OSMGeocodingResult * OSMGeocoding::reverseCoding(QPointF coord) {
|
||||
auto loc_c = QLocale::c();
|
||||
QNetworkRequest req(
|
||||
provider +
|
||||
QString("/reverse?lat=%1&lon=%2&format=json").arg(loc_c.toString(coord.x(), 'f', 8)).arg(loc_c.toString(coord.y(), 'f', 8)));
|
||||
req.setHeader(QNetworkRequest::UserAgentHeader, "Qt/5");
|
||||
req.setRawHeader("Accept-Language", (QLocale().name() + "," + QLocale().bcp47Name()).toLatin1());
|
||||
auto * r = nam->get(req);
|
||||
if (!r) return nullptr;
|
||||
OSMGeocodingResult * ret = new OSMGeocodingResult(nam);
|
||||
ret->reply = r;
|
||||
connect(r, &QNetworkReply::finished, this, [r, ret]() {
|
||||
r->deleteLater();
|
||||
if (r->error() != QNetworkReply::NoError) {
|
||||
qDebug() << "Error:" << r->error();
|
||||
} else {
|
||||
QByteArray data = r->readAll();
|
||||
if (!data.isEmpty()) ret->res = QJsonDocument::fromJson(data);
|
||||
}
|
||||
ret->requestDone();
|
||||
});
|
||||
return ret;
|
||||
}
|
||||
84
libs/map/osm_geocoding.h
Normal file
84
libs/map/osm_geocoding.h
Normal file
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
QAD - Qt ADvanced
|
||||
|
||||
Ivan Pelipenko peri4ko@yandex.ru
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef osm_geocoding_h
|
||||
#define osm_geocoding_h
|
||||
|
||||
#include "qad_map_export.h"
|
||||
|
||||
#include <QGeoCoordinate>
|
||||
#include <QJsonDocument>
|
||||
#include <QPointF>
|
||||
|
||||
class QNetworkReply;
|
||||
class QNetworkAccessManager;
|
||||
|
||||
|
||||
class QAD_MAP_EXPORT OSMGeocodingResult: public QObject {
|
||||
Q_OBJECT
|
||||
friend class OSMGeocoding;
|
||||
|
||||
public:
|
||||
void abort();
|
||||
|
||||
bool isEmpty() const { return res.isEmpty(); }
|
||||
QJsonDocument result() const { return res; }
|
||||
QString displayName() const;
|
||||
QPointF resultCoordinate() const;
|
||||
|
||||
private:
|
||||
OSMGeocodingResult(QObject * parent);
|
||||
~OSMGeocodingResult();
|
||||
|
||||
void requestDone();
|
||||
QJsonDocument res;
|
||||
QNetworkReply * reply = nullptr;
|
||||
|
||||
signals:
|
||||
void ready();
|
||||
};
|
||||
|
||||
|
||||
class QAD_MAP_EXPORT OSMGeocoding: public QObject {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
static OSMGeocodingResult * queue(QPointF coord);
|
||||
static OSMGeocodingResult * queue(QGeoCoordinate coord) { return queue(QPointF(coord.latitude(), coord.longitude())); }
|
||||
|
||||
private:
|
||||
explicit OSMGeocoding();
|
||||
~OSMGeocoding();
|
||||
|
||||
static OSMGeocoding * instance();
|
||||
|
||||
OSMGeocodingResult * reverseCoding(QPointF coord);
|
||||
|
||||
QNetworkAccessManager * nam = nullptr;
|
||||
QString provider;
|
||||
|
||||
public slots:
|
||||
|
||||
private slots:
|
||||
|
||||
signals:
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
@@ -1,6 +1,7 @@
|
||||
#include "pivariant_geoposition.h"
|
||||
|
||||
#include "mapitemimage.h"
|
||||
#include "osm_geocoding.h"
|
||||
#include "piqt.h"
|
||||
#include "pivaluetree.h"
|
||||
#include "ui_pivariant_geoposition.h"
|
||||
@@ -31,7 +32,18 @@ PIVariantEditors::GeoPosition::GeoPosition() {
|
||||
item->setScale(0.333 * appScale(this));
|
||||
item->setPosition(ui.map->center());
|
||||
ui.map->addItem(item);
|
||||
connect(ui.map, &MapView::mapClicked, this, [item](QPointF c) { item->setPosition(c); });
|
||||
OSMGeocodingResult * gc_res = nullptr;
|
||||
connect(ui.map, &MapView::mapClicked, this, [this, item, &gc_res, ui](QPointF c) {
|
||||
item->setPosition(c);
|
||||
if (gc_res) gc_res->abort();
|
||||
gc_res = OSMGeocoding::queue(c);
|
||||
ui.label->setText("-");
|
||||
connect(gc_res, &OSMGeocodingResult::ready, this, [&gc_res, ui]() {
|
||||
ui.label->setText(gc_res->displayName());
|
||||
gc_res = nullptr;
|
||||
});
|
||||
});
|
||||
ui.map->mapClicked(ui.map->center());
|
||||
if (dlg.exec() != QDialog::Accepted) return;
|
||||
PIGeoPosition gp({item->position().x(), item->position().y(), 0.}, PIGeoPosition::Geodetic);
|
||||
setValue(PIVariant::fromValue(gp));
|
||||
|
||||
@@ -16,11 +16,27 @@
|
||||
<layout class="QVBoxLayout" name="verticalLayout">
|
||||
<item>
|
||||
<widget class="MapView" name="map">
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="Preferred" vsizetype="Expanding">
|
||||
<horstretch>0</horstretch>
|
||||
<verstretch>0</verstretch>
|
||||
</sizepolicy>
|
||||
</property>
|
||||
<property name="zoom">
|
||||
<double>15.000000000000000</double>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLabel" name="label">
|
||||
<property name="text">
|
||||
<string>-</string>
|
||||
</property>
|
||||
<property name="wordWrap">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QDialogButtonBox" name="buttonBox">
|
||||
<property name="standardButtons">
|
||||
|
||||
Reference in New Issue
Block a user