Skip to main content

Getting location in Blackberry 10

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
LIBS += -lQtLocationSubset
To 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.
QGeoPositionInfoSource *src = QGeoPositionInfoSource::createDefaultSource(this);
There are 3 positioning methods that you can use to find location.
1) All positioning methods (trying GNSS and network methods)
2) Satellite positioning methods (only tries GNSS methods)
3) Non satellite positioning methods (tries WiFi and cell tower positioning methods)
Complete details of these methods can be found here.

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->setPreferredPositioningMethods
(QGeoPositionInfoSource::SatellitePositioningMethods);
We 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
src->setProperty("provider", "network");
src->setProperty("fixType", "wifi");
We can set many properties of QGeoPositionInfoSource using setProperty method. All details can be found here.

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 &)),
             this, SLOT(positionUpdated(const QGeoPositionInfo &)));
connect(src,SIGNAL(updateTimeout()),
             this,SLOT(positionUpdateTimedout()));
Sample implementation of positionUpdated slot that handles positionUpdated signal
void MyLocationHandingClass::positionUpdated(
                      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
    }
}
You can read about QGeoPositionInfo class here.

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 timeout
Retrieving 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 seconds
src->startUpdates();
To stop getting location we can use stopUpdates method.
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

Popular posts from this blog

Decoding JPEG image file using libavcodec

I got a chance to work on a video encoding application that decodes series of jpeg files and convert them into ogg theora video file. I used the infamous libavcodec library that is used in FFMPEG . I decided to write blog posts explaining how I decode jpeg images and convert them into ogg video file. This is the first part and in this I will explain how to decode jpeg images using libavcodec. To learn how to write decoded images as a ogg video file please read http://random-stuff-mine.blogspot.com/2017/07/encoding-raw-images-to-ogg-theora-video.html Before reading this blog post you must be aware of using and setting up libavcodec. I highly recommend this tutorial to get basics of using libavcodec http://www.ffmpeg.org/doxygen/0.6/api-example_8c-source.html Allocating input format context We will first allocate input format for reading the file. We will use avformat_open_input function that will allocate AVFormatContext structure passed to it , the function detects input typ

CryptographicException: An error occurred while trying to encrypt the provided data. Refer to the inner exception for more information

I created a new Blazor Server app in Visual Studio 2019 and tried to run it. But I was getting this error CryptographicException: An error occurred while trying to encrypt the provided data. Refer to the inner exception for more information. I couldn't find any reason or solution to this problem. I tried creating the project multiple times but same error. I created a new .Net Core Web App and added a new razor component and included that component in a razor page (cshtml file) like this @(await Html.RenderComponentAsync<GeofenceWork>(RenderMode.ServerPrerendered)) and <component type="typeof(GeofenceWork)" render-mode="serverprerendered" /> As soon as I navigate to this page that has component added I got the same error: CryptographicException: An error occurred while trying to encrypt the provided data. Refer to the inner exception for more information. This was very frustrating. After hours of trying and searching I figured out the solution. 

Encoding raw images to Ogg Theora video using libavcodec

In one of the blog posts we learned how to decode jpeg images using libavcodec. This is the second part of that post. In this we will learn how to encode decoded images (raw images) to theora and write them in ogg video file. In the end of the first part we saved our raw image in raw_data variable and its length in raw_data_size variable. Lets assume that we packaged all our decoding code in one function called "decode_jpeg_image" which has following signature int decode_jpeg_image(char *filename,int file_name_size,uint8_t *raw_data,int *raw_data_size) filename = name of jpeg file to decode file_name_size = length of jpeg file's name raw_data = contains decoded raw image on return raw_data_size = contains length of raw_data on return Now let's start working on how to encode this image in raw_data to theora and write that image to ogg video file. Finding Theora encoder We first have to find encoder for THEORA which is represented by AVCodec structure. He