Cerin
Cerin

Reputation: 64739

Improving Android GPS performance

How do you improve slow GPS signal acquisition on the Android platform?

I'm testing a simple GPS logger based on this open source code, and although it seems to work, it can take up to 10-15 minutes for it to first acquire a signal and start showing GPS coordinates. However, running the Google Maps app on the same device appears to acquire a signal almost instantly (it's even able to detect which direction I'm facing in realtime), while the GPS logger service still says it can't find a signal.

Why is Google Maps so fast at acquiring a GPS signal, while the standard GPS system service takes forever?

The specific code I have that starts the GPS service is:

private void startLoggerService() {
    lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locationListener = new MyLocationListener();
    lm.requestLocationUpdates(
        LocationManager.GPS_PROVIDER, 
        2000, 
        1,
        locationListener);
}

Upvotes: 3

Views: 4926

Answers (3)

mwengler
mwengler

Reputation: 2778

If google maps is showing your direction, it must be using a GPS fix. Google maps must be using the most recent gps fix which you can get using Location.getLastKnownLocation. Presumably if that location is not too old Google maps decides it is way better than nothing and shows it to you while you wait for new GPS fixes.

GPS fixes will often come really slowly or not at all when you are indoors. I assume as you develop you are trying to get GPS fixes? You may have to get up and walk outside.

Many phones have fairly lousy gps receivers. Even in clear outdoor sky I own phones which will take MANY minutes to get the first GPS fix. It sucks and its a rip-off, but its true.

Upvotes: 0

MH.
MH.

Reputation: 45493

There are a couple of things you should be aware of and definitely take into consideration when it comes to using positional data:

  • Don't rely on just the GPS sensor. Although GPS can potentially give you the most accurate result, it is relatively slow and quite the battery hog. In stead, also request location updates from the network provider: LocationManager.NETWORK_PROVIDER. In most cases this will give a result accurate enough to use in your app (most devices I've played with seem to yield a worst case accuracy of roughly 60~65m), but more importantly: it's much, much faster. Do note that just as with the GPS, users can enable and disable feature themselves in the system settings. For more details, see Using the Location Manager.
  • Think about how often you need a location update. If possible, prevent continuous updates (that'll drain the battery in no-time) and release the sensors as soon as you've received the data you require. Also, a good practice is to cache the location to some extend - even if it's not completely accurate, users do get feedback immediately. You can potentially combine this with some logic that takes the time stamp into account.
  • Use the resources that are already available on the Android Developer website. For example, there is a topic on Location Strategies that will be worth reading. Another good resource will be the blog post by Reto Meier and his open source project that implements his Android Protips for Location. If you have the time, also go over his Google I/O 2011 presentation that discusses best-practices for location-based/location-aware Android apps.

On a side note: the realtime indication of what direction you're facing has nothing to do with locations, but comes from either the magnetic field sensor (read: digital compass) or gyroscope. Both deal with the device's orientation, not position.

Upvotes: 8

The Applicationist
The Applicationist

Reputation: 525

The 1 in your code means that the provider will only broadcast the location should your device move by 1 meter. Try setting this to 0.

The API documentation states:

"The frequency of notification may be controlled using the minTime and minDistance parameters. If minTime is greater than 0, the LocationManager could potentially rest for minTime milliseconds between location updates to conserve power. If minDistance is greater than 0, a location will only be broadcasted if the device moves by minDistance meters. To obtain notifications as frequently as possible, set both parameters to 0."

Upvotes: 2

Related Questions