12-10-2012 10:45 AM - edited 12-10-2012 10:50 AM
Here is my little location handler
#include "LocationDiagnostics.hpp"
GPShandler::GPShandler(QObject* parent)
: QObject(parent)
, m_latitude(0)
, m_longitude(0)
, m_positionSource(QGeoPositionInfoSource::createDef aultSource(this))
{
if (m_positionSource) {
connect(m_positionSource, SIGNAL(positionUpdated(const QGeoPositionInfo &)), this, SLOT(positionUpdated(const QGeoPositionInfo &)));
//log(tr("Initialized QGeoPositionInfoSource"));
} else {
//log(tr("Failed to initialized QGeoPositionInfoSource"));
}
}
void GPShandler::startUpdates()
{
if (m_positionSource) {
m_positionSource->startUpdates();
//log(tr("Position updates started"));
}
}
void GPShandler::requestUpdate()
{
if (m_positionSource) {
m_positionSource->requestUpdate(120000);
//log(tr("Update requested"));
}
}
void GPShandler::stopUpdates()
{
if (m_positionSource) {
m_positionSource->stopUpdates();
//log(tr("Position updates stopped"));
}
deleteLater();
}
void GPShandler::positionUpdated(const QGeoPositionInfo& pos)
{
m_latitude = pos.coordinate().latitude();
m_longitude = pos.coordinate().longitude();
emit dataChanged();
}
QGeoPositionInfoSource* GPShandler::positionSource() const
{
return m_positionSource;
}
double GPShandler::latitude() const
{
return m_latitude;
}
double GPShandler::longitude() const
{
return m_longitude;
}
#ifndef GPSHANDLER_H_ #define GPSHANDLER_H_ #include#include #include #include #include #include using namespace QtMobilitySubset; class GPShandler : public QObject{ Q_OBJECT Q_PROPERTY(double latitude READ latitude NOTIFY dataChanged) Q_PROPERTY(double longitude READ longitude NOTIFY dataChanged) public: GPShandler(QObject* parent); //virtual ~GPShandler(); Q_INVOKABLE void startUpdates(); // This method is called to trigger an one-time retrieval of location information Q_INVOKABLE void requestUpdate(); // The accessor method for the internal geo position object QGeoPositionInfoSource* positionSource() const; // This method is called to stop the retrieval of location information Q_INVOKABLE void stopUpdates(); double latitude() const; double longitude() const; Q_SIGNALS: // The change notification signals of the properties void dataChanged(); private Q_SLOTS: void positionUpdated(const QGeoPositionInfo & pos); private: QGeoPositionInfoSource *m_positionSource; double m_latitude; double m_longitude; }; #endif /* GPSHANDLER_H_ */
in main.cpp
qmlRegisterType<GPShandler>();
GPShandler* test=new GPShandler(&app);
test->positionSource()->setPreferredPositioningMet hods(QGeoPositionInfoSource::AllPositioningMethods );
QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(&a pp);
qml->setContextProperty("_location", test);
In main.qml this function startNav() is trigerred on button click.
function sendCoordinates(lat,lon)
{
var l = (lat) + "," + (lon)
cleaningSearcher.navCode = l //passing data in this format: xxxxxx,xxxxxxx which is then parsed in c++, this part works ok
showListViewAnimation.play()
}
function startNav()
{
_location.requestUpdate()
onDataChanged: {
console.log("COORDINATE-==============")
console.log(_location.latitude) // ALWAYS RETURN 0 ?????
console.log(_location.longitude)
console.log("COORDINATE-==============")
root.sendCoordinates(_location.latitude,_location. longitude)
}
}
But these always return 0 on simulator, permissions are set, location services are on. I have no idea where to look anymore ![]()
_location.latitude and _location.longitude
12-10-2012 12:51 PM
It seems that Location services degraded significantly in Beta 4. There's likely nothing wrong with your code. Check out this thread http://supportforums.blackberry.com/t5/Cascades-De
12-10-2012 01:14 PM
12-10-2012 05:58 PM
I don't see the problem. Did you try initializing m_latitude and m_longitude with something other than 0 to see if they are not actually getting changed, as opposed to being determined as 0? Might give a clue.
12-10-2012 06:19 PM
12-12-2012 03:39 PM
Full qml
function sendCoordinates(lat,lon) { var l = (lat) + "," + (lon) cleaningSearcher.navCode = l showListViewAnimation.play() } function startSearch() { showListViewAnimation.play() cleaningSearcher.zipCode = zipCodeField.text } function startNav() { _location.requestUpdate() onDataChanged: { console.log("COORDINATE-==============") console.log(_location.latitude) console.log(_location.longitude) console.log("COORDINATE-==============") root.sendCoordinates(_location.latitude,_location.longitude) } } layout: DockLayout {} ImageButton { defaultImageSource: "asset:///images/getLocation.png" pressedImageSource: "asset:///images/getLocation.png" horizontalAlignment: HorizontalAlignment.Left verticalAlignment: VerticalAlignment.Bottom translationX: 5.0 translationY: -5.0 onClicked: root.startNav() }
On first click on the button, location returned is 0 or whatever lat and lon are initialised in constructor. On second click location is correctly returned to what is given by the simulator controller. I change location in controller to other values, click the button - location changes are not returned, returns prvious values. Click once more - location values are changed.
How to solve this now ?
12-13-2012 06:20 AM
12-13-2012 02:11 PM
12-14-2012 01:16 PM
Hello,
Does the locationdiagnostics app update the location as expected in your environment?
Shadid
12-14-2012 01:37 PM