gian1200
gian1200

Reputation: 3854

LocationRequest and LocationClient.requestLocationUpdates

I have an app that uses Google Maps and the new Fuse Location API. I use this code to getUpdates when the location changes:

locationClient.requestLocationUpdates(new LocationRequest(),
    new LocationListener() {

    @Override
    public void onLocationChanged(Location location) {
        //Some code
    }
});

I have no problem in my Galaxy Nexus with Android 4.3. However in my friend's phone (Xperia ST21i) with Android 4.0.4 I can't receive those updates.

I have seen on the Android documentation and on many (if not all) questions on StackOVerflow the use of LocationRequest.create() instead of new LocationRequest(). This new code still works fine on my phone; I haven't tried it on my friend's phone yet.

Reading the documentation, my guess is that LocationRequest.create() is just something like:

public static LocationRequest create() { 
    LocationRequest locationRequest= new LocationRequest();
    locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
    locationRequest.setFastestInterval(aNumber);
    locationRequest.setInterval(anotherNumber);
    return locationRequest;
}

What is the difference between this two pieces of code and why it works on my phone, but it doesn't on my friend's. Will it work after the change on my friend's phone?

If not, what else will I need to do to make it work?

Upvotes: 1

Views: 3633

Answers (2)

gian1200
gian1200

Reputation: 3854

I managed to solve my problem. I forgot that being connected (onConnected) doesn't mean that the location is known. However, I still don't know why my code wasn't working after telling my friend to open the Google Maps App and wait until it starts showing the current location, before opening my app. Maybe the Google Maps App doesn't use or update directly or indirectly the location of the Google Fusion API.

If you want to know how exactly I implemented it, here is a link: MapActivity

UPDATE: MapActivity

Upvotes: 0

syklon
syklon

Reputation: 188

There's actually an issue in many 4.0.x devices where OnLocationChanged doesn't get called properly if you rely on Network level location.

Issue report at AOSP Issue Tracker is likely what you're seeing

As stated there, basically the Network level location never refreshes, and since you're using LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY it will try to use the least obtrusive method to give you updates when possible. I do find it a little bit strange that you still never get updates if GPS and Wifi are available on that device as well.

A possible way to work around this issue is to change the priority to LocationRequest.PRIORITY_HIGH_ACCURACY. If you believe that solution will be troublesome to your users, you may do something like creating a new LocationRequest with PRIORITY_HIGH_ACCURACY and only using it if your first approach fails.

Particularly, I would set a timer when you first initiate the location request, and use a flag in your LocationListener to indicate whether or not any results are received in that time frame. Code examples can be supplied if necessary.

Upvotes: 2

Related Questions