2 Situare - A location system for Facebook
3 Copyright (C) 2010 Ixonos Plc. Authors:
5 Henri Lampela - henri.lampela@ixonos.com
6 Sami Rämö - sami.ramo@ixonos.com
8 Situare is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 version 2 as published by the Free Software Foundation.
12 Situare is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Situare; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
25 #include "coordinates/geocoordinate.h"
27 #include "network/networkaccessmanager.h"
29 #include "geocodingservice.h"
31 GeocodingService::GeocodingService(QObject *parent)
34 qDebug() << __PRETTY_FUNCTION__;
36 m_networkManager = new NetworkAccessManager(this);
37 connect(m_networkManager, SIGNAL(finished(QNetworkReply*)),
38 this, SLOT(requestFinished(QNetworkReply*)), Qt::QueuedConnection);
41 void GeocodingService::parseSearchResults(const QByteArray &jsonReply)
43 qDebug() << __PRETTY_FUNCTION__;
45 const QString NO_DATA = "ZERO_RESULTS";
47 m_searchResults.clear();
51 QVariantMap result = parser.parse(jsonReply, &ok).toMap();
53 emit error(ErrorContext::SITUARE, SituareError::ERROR_LOCATION_SEARCH_FAILED);
55 } else if(result.value("status") != NO_DATA) {
56 foreach(QVariant hitVariant, result["results"].toList()) {
58 QMap<QString, QVariant> hitMap = hitVariant.toMap();
59 location.setFormattedAddress(hitMap["formatted_address"].toString());
62 QVariant geometryVariant = hitMap["geometry"];
63 QMap<QString, QVariant> geometryMap = geometryVariant.toMap();
64 QVariant locationVariant = geometryMap["location"];
65 QMap<QString, QVariant> locationMap = locationVariant.toMap();
66 location.setCoordinates(GeoCoordinate(locationMap["lat"].toDouble(),
67 locationMap["lng"].toDouble()));
69 // parse recommended viewport
70 QVariant viewportVariant = geometryMap["viewport"];
71 QMap<QString, QVariant> viewportMap = viewportVariant.toMap();
72 QVariant swVariant = viewportMap["southwest"];
73 QMap<QString, QVariant> swMap = swVariant.toMap();
74 QVariant neVariant = viewportMap["northeast"];
75 QMap<QString, QVariant> neMap = neVariant.toMap();
77 GeoCoordinate southwest = GeoCoordinate(swMap["lat"].toDouble(),
78 swMap["lng"].toDouble());
80 GeoCoordinate northeast = GeoCoordinate(neMap["lat"].toDouble(),
81 neMap["lng"].toDouble());
83 location.setViewPort(southwest, northeast);
85 m_searchResults.append(location);
88 emit locationDataParsed(m_searchResults);
90 emit error(ErrorContext::SITUARE, SituareError::ERROR_LOCATION_SEARCH_FAILED);
94 void GeocodingService::requestFinished(QNetworkReply *reply)
96 qDebug() << __PRETTY_FUNCTION__;
98 QByteArray replyArray = reply->readAll();
101 emit error(ErrorContext::NETWORK, reply->error());
103 parseSearchResults(replyArray);
105 reply->deleteLater();
108 void GeocodingService::requestLocation(const QString &location)
110 qDebug() << __PRETTY_FUNCTION__;
112 const QString GEOCODE_SERVICE_ADDRESS = "http://maps.google.com/maps/api/geocode/json?address=";
113 const QString DEVICE_CONTAINS_LOCATION_SENSOR = "&sensor=true";
115 QString url = GEOCODE_SERVICE_ADDRESS;
116 url.append(location);
117 url.append(DEVICE_CONTAINS_LOCATION_SENSOR);
122 void GeocodingService::sendRequest(const QUrl &url)
124 qDebug() << __PRETTY_FUNCTION__;
126 QNetworkRequest request;
129 request.setAttribute(QNetworkRequest::CacheSaveControlAttribute, false);
132 m_networkManager->post(request, ba, false);