5 Geocode::Geocode(QObject *parent) :
12 QStringList providers = QGeoServiceProvider::availableServiceProviders();
14 if (!providers.isEmpty()) {
15 m_provider = new QGeoServiceProvider(providers.at(0));
18 qCritical() << "Cannot find any geo-service providers";
22 m_manager = m_provider->searchManager();
24 qCritical() << "Cannot get hold of the geo-search manager" << m_provider->errorString();
28 if (!m_manager->supportsReverseGeocoding()) {
29 qCritical() << "geo-search manager does not support reverse geocoding";
31 delete m_provider; m_provider = 0;
36 QObject::connect(m_manager, SIGNAL(finished(QGeoSearchReply *)),
37 this, SLOT(finished(QGeoSearchReply *)));
38 QObject::connect(m_manager, SIGNAL(error(QGeoSearchReply *, const QGeoSearchReply::Error&,
40 this, SLOT(error(QGeoSearchReply *, const QGeoSearchReply::Error&,
47 delete m_provider; m_provider = 0;
50 bool Geocode::isActive() const {
54 void Geocode::setActive(bool active) {
55 if (active != m_active) {
58 if (!m_active && m_reply) {
59 m_reply->deleteLater(); m_reply = 0;
68 QString Geocode::country() const {
72 QString Geocode::city() const {
76 QString Geocode::suburb() const {
80 void Geocode::search(double longitude, double latitude) {
81 if (!m_active || !m_provider) {
90 m_reply = m_manager->reverseGeocode(QGeoCoordinate(latitude, longitude));
92 qCritical() << "geo-search manager provided a null reply!";
97 void Geocode::clear() {
98 if (!m_country.isEmpty()) {
100 emit countryChanged();
103 if (!m_city.isEmpty()) {
108 if (!m_suburb.isEmpty()) {
110 emit suburbChanged();
114 void Geocode::finished(QGeoSearchReply *reply) {
115 if (reply->error() != QGeoSearchReply::NoError) {
116 qWarning() << "Error while geocoding" << reply->error() << reply->errorString();
118 reply->deleteLater();
120 if (reply == m_reply) {
129 QList<QGeoPlace> places = reply->places();
130 if (!places.isEmpty()) {
131 QGeoAddress address = places.at(0).address();
132 if (m_country != address.country()) {
133 m_country = address.country();
134 emit countryChanged();
137 if (m_city != address.city()) {
138 m_city = address.city();
142 if (m_suburb != address.district()) {
143 m_suburb = address.district();
144 emit suburbChanged();
148 qWarning() << "No places found";
152 reply->deleteLater();
154 if (reply == m_reply) {
159 void Geocode::error(QGeoSearchReply *reply, const QGeoSearchReply::Error& error,
160 const QString& errorString) {
162 qWarning() << "Error while geocoding" << error << reply->errorString() << errorString;
164 reply->deleteLater();
166 if (reply == m_reply) {