Skip to content

Commit

Permalink
初始提交
Browse files Browse the repository at this point in the history
  • Loading branch information
Sinden committed Jun 22, 2017
0 parents commit 32b8aba
Show file tree
Hide file tree
Showing 33 changed files with 2,609 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .qmake.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
load(qt_build_config)

CONFIG += warning_clean
MODULE_VERSION = 5.9.0
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2016 Vladyslav Stelmakhovskyi http://www.vladest.org

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Amap Maps plugin for QtLocation
¸ßµÂµØͼ plugin for QtLocation module

44 changes: 44 additions & 0 deletions amap.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
TARGET = qtgeoservices_amap
QT += location-private positioning-private network

PLUGIN_TYPE = geoservices
PLUGIN_CLASS_NAME = QGeoServiceProviderFactoryAmap
load(qt_plugin)

HEADERS += \
qgeotilefetcheramap.h \
qplacesearchsuggestionreplyimpl.h \
qgeoerror_messages.h \
qgeocodereplyamap.h \
qgeocodingmanagerengineamap.h \
qgeomapreplyamap.h \
qgeoroutereplyamap.h \
qgeoroutingmanagerengineamap.h \
qgeoserviceproviderpluginamap.h \
qgeotiledmapamap.h \
qgeotiledmappingmanagerengineamap.h \
qgeotilefetcheramap.h \
qplacecategoriesreplyamap.h \
qplacemanagerengineamap.h \
qplacesearchreplyamap.h

SOURCES += \
qplacesearchsuggestionreplyimpl.cpp \
qgeoerror_messages.cpp \
qgeocodereplyamap.cpp \
qgeocodingmanagerengineamap.cpp \
qgeomapreplyamap.cpp \
qgeoroutereplyamap.cpp \
qgeoroutingmanagerengineamap.cpp \
qgeoserviceproviderpluginamap.cpp \
qgeotiledmapamap.cpp \
qgeotiledmappingmanagerengineamap.cpp \
qgeotilefetcheramap.cpp \
qplacecategoriesreplyamap.cpp \
qplacemanagerengineamp.cpp \
qplacesearchreplyamap.cpp


OTHER_FILES += \
amap_plugin.json

14 changes: 14 additions & 0 deletions amap_plugin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"Keys": ["amap"],
"Provider": "amap",
"Version": 100,
"Experimental": false,
"Features": [
"OnlineGeocodingFeature",
"ReverseGeocodingFeature",
"OnlineRoutingFeature",
"AlternativeRoutesFeature",
"OnlineMappingFeature",
"SearchSuggestionsFeature"
]
}
157 changes: 157 additions & 0 deletions qgeocodereplyamap.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
#include "qgeocodereplyamap.h"


#include <QtPositioning/QGeoCoordinate>
#include <QtPositioning/QGeoAddress>
#include <QtPositioning/QGeoLocation>
#include <QtPositioning/QGeoRectangle>

#include <QtCore/QJsonDocument>
#include <QtCore/QJsonArray>
#include <QtCore/QJsonObject>

static QGeoCoordinate constructCoordiante(const QJsonObject &jsonCoord) {
QGeoCoordinate coord(0,0);
coord.setLatitude(jsonCoord.value(QStringLiteral("lat")).toDouble());
coord.setLongitude(jsonCoord.value(QStringLiteral("lng")).toDouble());
return coord;
}

static bool checkAddressType(const QJsonObject &jsonAddress, const QString &type) {
QJsonArray a = jsonAddress.value("types").toArray();
for (int i = 0; i < a.size(); i++) {
if (a.at(i).toString() == type)
return true;
}
return false;
}

QGeoCodeReplyAmap::QGeoCodeReplyAmap(QNetworkReply *reply, QObject *parent)
: QGeoCodeReply(parent), m_reply(reply)
{
connect(m_reply, SIGNAL(finished()), this, SLOT(networkReplyFinished()));
connect(m_reply, SIGNAL(error(QNetworkReply::NetworkError)),
this, SLOT(networkReplyError(QNetworkReply::NetworkError)));

setLimit(1);
setOffset(0);
}

QGeoCodeReplyAmap::~QGeoCodeReplyAmap()
{
if (m_reply)
m_reply->deleteLater();
}

void QGeoCodeReplyAmap::abort()
{
if (!m_reply)
return;

m_reply->abort();

m_reply->deleteLater();
m_reply = 0;
}


void QGeoCodeReplyAmap::networkReplyFinished()
{
if (!m_reply)
return;

if (m_reply->error() != QNetworkReply::NoError)
return;

QString status;

QList<QGeoLocation> locations;
// setError(QGeoCodeReply::ParseError, QStringLiteral("Error parsing OpenRouteService xml response:") + xml.errorString() + " at line: " + xml.lineNumber());
QJsonDocument document = QJsonDocument::fromJson(m_reply->read(m_reply->bytesAvailable()));
if (document.isObject()) {
QJsonObject object = document.object();

status = object.value(QStringLiteral("status")).toString();
if (status == "OK") {
QJsonArray jsonlocations = object.value(QStringLiteral("results")).toArray();
qDebug() << "locations:" << jsonlocations.size();
for(int i = 0; i < jsonlocations.size(); i++) {
QGeoLocation location;
QGeoAddress address;

QJsonObject o = jsonlocations.at(i).toObject();
address.setText(o.value("formatted_address").toString());
QJsonObject ogeometry = o.value("geometry").toObject();

location.setCoordinate(constructCoordiante(ogeometry.value("location").toObject()));

QJsonObject jaddressRanges = ogeometry.value("viewport").toObject();
if (!jaddressRanges.isEmpty()) {
QGeoRectangle r;
r.setTopRight(constructCoordiante(jaddressRanges.value("northeast").toObject()));
r.setBottomLeft(constructCoordiante(jaddressRanges.value("southwest").toObject()));
location.setBoundingBox(r);
}

QJsonArray jaddress = o.value("address_components").toArray();

QString street_name;
QString street_num;
for(int j = 0; j < jaddress.size(); j++) {
QJsonObject addobj = jaddress.at(j).toObject();
if (checkAddressType(addobj, "street_number"))
street_num = addobj.value("long_name").toString();
if (checkAddressType(addobj, "route"))
street_name = addobj.value("long_name").toString();
if (checkAddressType(addobj, "country"))
address.setCountryCode(addobj.value("short_name").toString());
if (checkAddressType(addobj, "administrative_area_level_3"))
address.setCity(addobj.value("long_name").toString());
if (checkAddressType(addobj, "administrative_area_level_2"))
address.setCounty(addobj.value("long_name").toString());
if (checkAddressType(addobj, "administrative_area_level_1"))
address.setState(addobj.value("long_name").toString());
if (checkAddressType(addobj, "sublocality"))
address.setDistrict(addobj.value("long_name").toString());
if (checkAddressType(addobj, "postal_code"))
address.setPostalCode(addobj.value("long_name").toString());
}
address.setStreet(street_name + (street_num.size() > 0 ? ( ", " + street_num) : ""));

location.setAddress(address);

locations.append(location);
}
}
} else {
if (status == "ZERO_RESULTS")
setError(CombinationError, "Geocode was successful but returned no results. This may occur if the geocoder was passed a non-existent address");
else if (status == "OVER_QUERY_LIMIT")
setError(CommunicationError, "Request quota is over");
else if (status == "REQUEST_DENIED")
setError(CommunicationError, "Request denied");
else if (status == "INVALID_REQUEST")
setError(UnsupportedOptionError, "Address, components or latlng is missing");
else if (status == "UNKNOWN_ERROR")
setError(UnknownError, "Request could not be processed due to a server error. Try again later");
}

setLocations(locations);
setFinished(true);

m_reply->deleteLater();
m_reply = 0;
}

void QGeoCodeReplyAmap::networkReplyError(QNetworkReply::NetworkError error)
{
Q_UNUSED(error)

if (!m_reply)
return;

setError(QGeoCodeReply::CommunicationError, m_reply->errorString());

m_reply->deleteLater();
m_reply = 0;
}
25 changes: 25 additions & 0 deletions qgeocodereplyamap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef QGEOCODEREPLYAMAP_H
#define QGEOCODEREPLYAMAP_H

#include <QtNetwork/QNetworkReply>
#include <QtLocation/QGeoCodeReply>

class QGeoCodeReplyAmap : public QGeoCodeReply
{
Q_OBJECT

public:
explicit QGeoCodeReplyAmap(QNetworkReply *reply, QObject *parent = 0);
~QGeoCodeReplyAmap();

void abort();

private Q_SLOTS:
void networkReplyFinished();
void networkReplyError(QNetworkReply::NetworkError error);

private:
QNetworkReply *m_reply;
};

#endif // QGEOCODEREPLYORS_H
Loading

0 comments on commit 32b8aba

Please sign in to comment.