Go to the documentation of this file.00001 #include <iostream>
00002 #include "geocode_data_manager.h"
00003
00004 #include <qjson/parser.h>
00005 #include <QDebug>
00006
00007 const QString apiKey = "AIzaSyCad4XfoHa-7R5qwSjq6zv4_9FmvHO96RY";
00008
00009 GeocodeDataManager::GeocodeDataManager(QObject *parent) :
00010 QObject(parent)
00011 {
00012 m_pNetworkAccessManager = new QNetworkAccessManager(this);
00013 connect(m_pNetworkAccessManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
00014
00015 }
00016
00017 void GeocodeDataManager::getCoordinates(const QString& address)
00018 {
00019 std::cout << 'url=' << std::endl;
00020 QString url = QString("http://maps.google.com/maps/geo?q=%1&key=%2&output=json&oe=utf8&sensor=false").arg(address).arg(
00021 apiKey);
00022 qWarning() << "url=" << url;
00023 m_pNetworkAccessManager->get(QNetworkRequest(QUrl(url)));
00024 }
00025
00026 void GeocodeDataManager::replyFinished(QNetworkReply* reply)
00027 {
00028 QString json = reply->readAll();
00029 qDebug() << "Reply = " << json;
00030 qDebug() << "URL = " << reply->url();
00031 QString strUrl = reply->url().toString();
00032
00033 QJson::Parser parser;
00034
00035 bool ok;
00036
00037
00038 QVariant result = parser.parse(json.toLatin1(), &ok);
00039 if (!ok)
00040 {
00041 emit errorOccured(QString("Cannot convert to QJson object: %1").arg(json));
00042 return;
00043 }
00044
00045 int code = result.toMap()["Status"].toMap()["code"].toInt();
00046 if (code != 200)
00047 {
00048 emit errorOccured(QString("Code of request is: %1").arg(code));
00049 return;
00050 }
00051
00052 QVariantList placeMarks = result.toMap()["Placemark"].toList();
00053 if (placeMarks.count() == 0)
00054 {
00055 emit errorOccured(QString("Cannot find any locations"));
00056 return;
00057 }
00058
00059 double east = placeMarks[0].toMap()["Point"].toMap()["coordinates"].toList()[0].toDouble();
00060 double north = placeMarks[0].toMap()["Point"].toMap()["coordinates"].toList()[1].toDouble();
00061
00062 emit coordinatesReady(east, north);
00063
00064 }