2 * This file is part of CameraPlus.
4 * Copyright (C) 2012-2013 Mohammed Sameer <msameer@foolab.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include <QStringList>
25 Geocode::Geocode(QObject *parent) :
32 QStringList providers = QGeoServiceProvider::availableServiceProviders();
34 if (!providers.isEmpty()) {
35 m_provider = new QGeoServiceProvider(providers.at(0));
38 qCritical() << "Cannot find any geo-service providers";
42 m_manager = m_provider->searchManager();
44 qCritical() << "Cannot get hold of the geo-search manager" << m_provider->errorString();
48 if (!m_manager->supportsReverseGeocoding()) {
49 qCritical() << "geo-search manager does not support reverse geocoding";
51 delete m_provider; m_provider = 0;
56 QObject::connect(m_manager, SIGNAL(finished(QGeoSearchReply *)),
57 this, SLOT(finished(QGeoSearchReply *)));
58 QObject::connect(m_manager, SIGNAL(error(QGeoSearchReply *, const QGeoSearchReply::Error&,
60 this, SLOT(error(QGeoSearchReply *, const QGeoSearchReply::Error&,
67 delete m_provider; m_provider = 0;
70 bool Geocode::isActive() const {
74 void Geocode::setActive(bool active) {
75 if (active != m_active) {
78 if (!m_active && m_reply) {
79 m_reply->deleteLater(); m_reply = 0;
88 QString Geocode::country() const {
92 QString Geocode::city() const {
96 QString Geocode::suburb() const {
100 void Geocode::search(double longitude, double latitude) {
101 if (!m_active || !m_provider) {
110 m_reply = m_manager->reverseGeocode(QGeoCoordinate(latitude, longitude));
112 qCritical() << "geo-search manager provided a null reply!";
117 void Geocode::clear() {
118 if (!m_country.isEmpty()) {
120 emit countryChanged();
123 if (!m_city.isEmpty()) {
128 if (!m_suburb.isEmpty()) {
130 emit suburbChanged();
134 void Geocode::finished(QGeoSearchReply *reply) {
135 if (reply->error() != QGeoSearchReply::NoError) {
136 qWarning() << "Error while geocoding" << reply->error() << reply->errorString();
138 reply->deleteLater();
140 if (reply == m_reply) {
149 QList<QGeoPlace> places = reply->places();
150 if (!places.isEmpty()) {
151 QGeoAddress address = places.at(0).address();
152 if (m_country != address.country()) {
153 m_country = address.country();
154 emit countryChanged();
157 if (m_city != address.city()) {
158 m_city = address.city();
162 if (m_suburb != address.district()) {
163 m_suburb = address.district();
164 emit suburbChanged();
168 qWarning() << "No places found";
172 reply->deleteLater();
174 if (reply == m_reply) {
179 void Geocode::error(QGeoSearchReply *reply, const QGeoSearchReply::Error& error,
180 const QString& errorString) {
182 qWarning() << "Error while geocoding" << error << reply->errorString() << errorString;
184 reply->deleteLater();
186 if (reply == m_reply) {