Speed up location acquisition on Android

Overview

Regarding the article that I wrote the other day, I used LocationManager to get the location information. At that time, slow speed became a problem, so I will write an article about how I could improve it.

The first implementation

python



private void callRequestLocationUpdates()
{
    checkPermission();
    mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 1, listener);
}

I used LocationManager.requestLocationUpdates () to get the location information on Android, but at first I implemented it as above. However, there seems to be a method other than GPS to acquire location information with LocationManager, and I found that it is controlled by the first argument of requestLocationUpdates (). It seems that the first argument is called "propider", but if you refer to the docs described in the reference, you can see that there are about 3 constants with the name "PROVIDER".

・ GPS_PROVIDER ・ NETWORK_PROVIDER ・ PASSIVE_PROVIDER

Since only one PROVIDER can be set in the argument of requestLocationUpdates (), I thought that I had to select one of the above three at first, but in fact, in LocationManager, requestLocationUpdates () is set in the above three PROVIDER. It is also possible to run in parallel.

Improved implementation

For parallel running of requestLocationUpdates (), I referred to the site described for reference (Android development | How to get location information in parallel with multiple providers). There was the following description.

It is passed to requestLocationUpdatesde to start measurement, but if you call this for the required provider, measurement will be performed in parallel.

In addition, there was the following description.

If all providers use the same listener, removeUpdates will stop measuring for all providers once called.

Oh! this is! I thought. In other words, I thought that if you turn it with a for statement and let all PROVIDER perform parallel processing, and if you can get the location information first and then perform subsequent processing, you can expect a higher speed for the entire application!

And the code that has been improved by referring to the above site is as follows.

python


private void callRequestLocationUpdates()
{
    //Aiming for high speed by rotating processing with multiple providers
    List<String> providers = mLocationManager.getProviders(true);
    checkPermission();
    for (String provider : providers) {
        mLocationManager.requestLocationUpdates(provider, 500, 1, listener);
    }
}

LocationListener.onLocationChanged () is called when the location information can be obtained by requestLocationUpdates (), but since it is parallel processing, it is not good if the subsequent processing is called many times, so devise a little. Is implemented.


@Override
public synchronized void onLocationChanged(Location location) {
    //Location information acquisition
    lat = location.getLatitude();
    lng = location.getLongitude();
    if(getActiveFlg() == 0){
        //Set a flag in the first process so that it will not be executed after the second process
        setActiveFlg(1);
        //Subsequent processing
        doSomething(lat, lng);
    }
}

With this, the subsequent processing will be fired only by onLocationChanged () that was started first, and it will not be fired after the second time. If you also add synchronized, you can rest assured that no other thread will interrupt the if statement before setActiveFlg (1).

As a result, subsequent processing can be performed only with PROVIDER, which was able to acquire the location information at the fastest speed, and as a result, the acquisition of location information could be speeded up.

reference: Get Android location information LocationManager Android development | How to get location information in parallel with multiple providers

Recommended Posts

Speed up location acquisition on Android
[Android] Notes on xml
Get Android location information
Customize list view on Android
Multipart transmission library on Android
Use serial communication on Android
ROS app development on Android
Set up Gitolite on CentOS 7
Ubuntu on Windows build speed
Use native code on Android
Set up ansible-playbook on Ubuntu 20.04