Location awareness is a required feature in most of the mobile phone applications today. Blackberry 10 API provides very easy way of accessing user's current location. In this blog post we will see how applications can access user location through native blackberry 10 API.
Before using Location API you must add following to your .pro file
We use QGeoPositionInfoSource class to find the location of a device and get updates about its position. To use QGeoPositionInfoSource you must include it like this
By default QGeoPositionInfoSource will have All positioning methods enabled. We can tell it explicitly which method to use by using setPreferredPositioningMethods function. For example if we want to use only Satellite positioning method we can do it like this
Connecting slots to signals of QGeoPositionInfoSource
We will connect two signals of QGeoPositionInfoSource to our class' slots.
Retrieving single fix
To find one time location we use requestUpdate method of QGeoPositionInfoSource, it takes milliseconds as parameter for timeout period.
To get location at regular intervals we use startUpdates method of QGeoPositionInfoSource. To set interval period we use setUpdateInterval method of QGeoPositionInfoSource which takes milliseconds as parameter.
Before using Location API you must add following to your .pro file
LIBS += -lQtLocationSubsetTo enable your app to obtain the current position or last known position of the device, you must add the access_location_services permission to your bar-descriptor.xml file like this
<permission>access_location_services</permission>Note that Location services must be enabled on device to get the location.
We use QGeoPositionInfoSource class to find the location of a device and get updates about its position. To use QGeoPositionInfoSource you must include it like this
<QtLocationSubset/QGeoPositionInfoSource>First we must initialize position source. which can be done through createDefaultSource method of QGeoPositionInfoSource class like this
//Set up the position info source.There are 3 positioning methods that you can use to find location.
QGeoPositionInfoSource *src = QGeoPositionInfoSource::createDefaultSource(this);
1) All positioning methods (trying GNSS and network methods)Complete details of these methods can be found here.
2) Satellite positioning methods (only tries GNSS methods)
3) Non satellite positioning methods (tries WiFi and cell tower positioning methods)
By default QGeoPositionInfoSource will have All positioning methods enabled. We can tell it explicitly which method to use by using setPreferredPositioningMethods function. For example if we want to use only Satellite positioning method we can do it like this
src->setPreferredPositioningMethodsWe can also specify to use any specific method through setProperty function. For example if we want to use only WiFi method we can do it like this
(QGeoPositionInfoSource::SatellitePositioningMethods);
src->setProperty("provider", "network");We can set many properties of QGeoPositionInfoSource using setProperty method. All details can be found here.
src->setProperty("fixType", "wifi");
Connecting slots to signals of QGeoPositionInfoSource
We will connect two signals of QGeoPositionInfoSource to our class' slots.
1) positionUpdated (Its emitted when position is found)
2) updateTimeout (Its emitted if timeout occurs in finding a location)
connect(src,SIGNAL(positionUpdated(const QGeoPositionInfo &)),Sample implementation of positionUpdated slot that handles positionUpdated signal
this, SLOT(positionUpdated(const QGeoPositionInfo &)));
connect(src,SIGNAL(updateTimeout()),
this,SLOT(positionUpdateTimedout()));
void MyLocationHandingClass::positionUpdated(You can read about QGeoPositionInfo class here.
const QGeoPositionInfo& geoPositionInfo)
{
if (geoPositionInfo.isValid())
{
QGeoCoordinate geoCoordinate = geoPositionInfo.coordinate();
qreal latitude = geoCoordinate.latitude();
qreal longitude = geoCoordinate.longitude();
//now do something with latitude and longitude
}
}
Retrieving single fix
To find one time location we use requestUpdate method of QGeoPositionInfoSource, it takes milliseconds as parameter for timeout period.
src->requestUpdates(15000); //request location with 15 secs timeoutRetrieving multiple fixes
To get location at regular intervals we use startUpdates method of QGeoPositionInfoSource. To set interval period we use setUpdateInterval method of QGeoPositionInfoSource which takes milliseconds as parameter.
src->setUpdateInterval(10000); //set interval of 10 secondsTo stop getting location we can use stopUpdates method.
src->startUpdates();
src->stopUpdates();If you want your app to receive location updates in background (when app is minimized etc). You must set canRunInBackground property to true using setProperty method of QGeoPositionInfoSource.
src->setProperty(“canRunInBackground", true);
Comments
Post a Comment
Share your wisdom