Skip to content Skip to sidebar Skip to footer

Android LocationListener Switching From GPS To Network

I have a Service implementing LocationListener listening for both GPS and Network. The application is dependant on a constant location-feed, but it seems when GPS has a hard time

Solution 1:

I agree with most of comments from AlexBottoni good answer, although in some points I can't suppot him.

Overview

First, just to check that you are doing it right...

You setup the same LocationListener for both providers. To indentify from where you are reciving the location you need to test it like this:

public void onLocationChanged(Location fix) {
    if(fix.getProvider().equals(LocationManager.GPS_PROVIDER)){
        //here Gps
    } else if(fix.getProvider().equals(LocationManager.NETWORK_PROVIDER)){
        //here Network
    }

Also, you setup a different acquisition frequency. Gps is providing a new location every 30 seconds and Network every 2 minutes.

As you didn't impose a minimum distance, you should receive a new Location from each one of the providers (as long as they can get a fix) with the frequency requested. If you don't receive a fix, is because they weren't able to acquire one.

Also, it may takes a little longer then requested to get the fix (mainly with Gps), because it may take some time to shyncronize with satellites and fix a location.

Fallback

There is no builted-in fallback from one provider to the other. They are independet, as said by Alex. I'm using the following approach to implement fallback:

  • Register Gps listener and start a timer
  • On every GPS location, restart timer
  • If timer reachs end, register Network listener (Gps listener keeps registered)
  • If new Gps location arrives, unregister Network listener, restart timer

Preferable Provider

Although Gps may not be available everyhere, is far most precise then Network. In my town, I get 6 meters accuracy with GPS and 1 Km with Network :-(

Two services

Doesn't matter where you register the listener, activity or service, separate ot together, as long as you request them and the provider can get a fix, you will get the location (assuming no bugs in application :-))

Final Notes

Ensure you have the permissions need (ACCESS_FINE_LOCATION, INTERNET, etc).

Ensure that phone setup have Network Location enabled (usually default is disable)

Most Gps receivers support updating information about satellite location, which improves fix time. You can use GPS Satus from the market, to do it.

Regards.


Solution 2:

This is really weird because, AFAIK, Android does not fall back to the second choice (network location provider) only if and when the first one (GPS) does not work. The two location providers are indipendent and should be sending location updates to you listeners indipendently to each other. Hence, the first one (GPS) should not be able to block the second one (network) in any case.

Regarding your questions:

How do I make sure that I always get a location?

You don't. There is no way to be sure. There are cases in which you cannot get any location fix just because no location reference is available. This is often the case in metro/underground stations. No GPS, no cellular antennas (not everybody lives in NYC or London...), no wi-fi hotspots so no way to determine your current position. Believe it or not, in the new hospital of my town, we have this situation at the moment because GPS constellation is not visible (indoor...), no wi-fi hotspots have been installed yet and the only available CellID antenna is just a few hundred meters away so you get the same signal everywhere, both outside and inside the building.

How can I make sure that if I dont get a GPS-location, I get a Network-location?

You cannot, either. It depends on the available networks. Anyway, you can check what you get from the location providers in your code and switch from one to the next until you get a usable location fix.

Is it a known bug?

No, it is not a bug. It is more a known limit of the existing technology.

Should I have 2 services, GPSLocationService and NetworkLocationsService?

It is something to try. As nick already said, this should not be a problem but a check should not hurt.

Is there a solution to this? :)

If your app is intended to be used mainly in a urban environment (a town), most likely you should use the network location engine as your main location engine. Nowadays, every village and town is covered by a large set on cellular phone antennas and by a large set of wi-fi hotspot so you are more likely to get a good location fix from the network location provider than from the GPS one. This is particularly true in towns with high buildings and narrow roads (that does not just mean NYC. Even here in Venice we have problems with the GPS). The network engine is also faster in getting a first fix and works indoor as well.

Fall back to GPS only if and when the network location engine does not work.


Post a Comment for "Android LocationListener Switching From GPS To Network"