Skip to content

Instantly share code, notes, and snippets.

@dio
Created August 2, 2011 06:52
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save dio/1119717 to your computer and use it in GitHub Desktop.
reverse geocode qtmobility
#include "geo.h"
#include <QDebug>
#include <QList>
Geo::Geo(QObject *parent) :
QObject(parent)
{
m_provider = new QGeoServiceProvider("nokia");
m_searchManager = m_provider->searchManager();
connect(m_searchManager, SIGNAL(finished(QGeoSearchReply*)), this, SLOT(searchFinished(QGeoSearchReply*)));
}
Geo::~Geo(){}
void Geo::findAddress()
{
qDebug() << "I'm searching now" << m_latitude << "-" << m_longitude;
QGeoCoordinate coord(m_latitude, m_longitude);
m_searchManager->reverseGeocode(coord);
}
void Geo::searchFinished(QtMobility::QGeoSearchReply *reply)
{
QList<QGeoPlace> places = reply->places();
if(places.size() > 0)
qDebug() << places.at(0).address().city(); //Jakarta
}
void Geo::searchError(QtMobility::QGeoSearchReply *reply, QGeoSearchReply::Error error, QString errorString)
{
qDebug() << errorString;
}
void Geo::setLatitude(double latitude){
m_latitude = latitude;
}
void Geo::setLongitude(double longitude){
m_longitude = longitude;
}
double Geo::latitude(){
return m_latitude;
}
double Geo::longitude(){
return m_longitude;
}
#ifndef GEO_H
#define GEO_H
#include <QObject>
#include <QGeoServiceProvider>
#include <QGeoSearchManager>
QTM_USE_NAMESPACE
class Geo : public QObject
{
Q_OBJECT
public:
explicit Geo(QObject *parent = 0);
~Geo();
Q_INVOKABLE void findAddress();
Q_PROPERTY(double latitude READ latitude WRITE setLatitude)
Q_PROPERTY(double longitude READ longitude WRITE setLongitude)
void setLatitude(double);
void setLongitude(double);
double latitude();
double longitude();
private:
QGeoServiceProvider * m_provider;
QGeoSearchManager * m_searchManager;
double m_longitude, m_latitude;
private slots:
void searchFinished(QGeoSearchReply *reply);
void searchError(QGeoSearchReply *reply, QGeoSearchReply::Error error, QString errorString);
signals:
public slots:
};
#endif // GEO_H
#include <QtGui/QApplication>
#include "qmlapplicationviewer.h"
#include <QDeclarativeComponent>
#include "geo.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
qmlRegisterType<Geo>("Geo",1,0,"Geo");
QmlApplicationViewer viewer;
viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
viewer.setMainQmlFile(QLatin1String("qml/geo/main.qml"));
viewer.showExpanded();
return app.exec();
}
import QtQuick 1.0
import Geo 1.0
import QtMobility.location 1.1
Rectangle {
width: 360
height: 360
MouseArea {
anchors.fill: parent
onClicked: {
Qt.quit();
}
}
PositionSource{
id: positionSource
active: true
}
Text {
text: positionSource.position.coordinate.latitude
anchors.centerIn: parent
}
Geo{
id: geo
latitude: positionSource.position.coordinate.latitude
longitude: positionSource.position.coordinate.longitude
}
Timer{
id: timer
interval: 2000
onTriggered: {
geo.findAddress();
timer.stop();
}
}
Component.onCompleted: {
// need a better checking -- if gps ready
timer.start();
}
}
@rajanmba
Copy link

rajanmba commented Aug 3, 2011

Hi
I get file error in
C:\QtSDK\geo\geo.h:6: error: QGeoServiceProvider: No such file or directory
C:\QtSDK\geo\geo.h:7: error: QGeoSearchManager: No such file or directory
Iam using Qt SDK 4.7.3 with Qt Creator 2.2.1 on win xp
Pl help in resolving the error
Thanks in advance

@dio
Copy link
Author

dio commented Aug 3, 2011

Hello!

Do you put
CONFIG += mobility
MOBILITY += location

in your .pro?

@rajanmba
Copy link

rajanmba commented Aug 4, 2011

I had done that but had forgotten to uncomment !! (The default pro file has the string commented). What a ridiculous mistake. THANK YOU

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment