views:

61

answers:

2

Hi,

My application requires current location of the user. For this I have implemented the code and I am able to get the current location. The code tries to get the best provider first and then fetches location information. The problem is that the current location is returned from NETWORK_PROVIDER if GPS_PROVIDER is disabled. If both NETWORK_PROVIDER and GPS_PROVIDER are enabled then the best provider will be GPS_PROVIDER and this takes too much time to get the current location.

So, is it alright if we set the location provider as NETWORK_PROVIDER only so that it will not matter whether GPS_PROVIDER is enabled or not.

Please let me know what is the right approach.

Regards

Sunil

A: 

Hi Sunil,

GPS gives better precision than Network provider.

Network provider is done using triangulation of cell towers and wifi.

So, if you need immediately, start with "last known location" then switch to network, once you have the fix from the gps, start using it.

regards, vinay

Vinay
+1  A: 

Depending on your application, you might be able to use just the network location. This is fine-grained enough for many location-based services. Plus you don't have to worry about sucking up the user's battery with GPS radio.

But if you really want as accurate a location as possible, why not listen to both providers? You could use the getLastKnownLocation() method for the initial location. Then you can use the NETWORK_PROVIDER location. Then, as soon as you get a fix from the GPS_PROVIDER, you can ignore or remove the network listener.

Neil Traft
getLastKnownLocation sometimes do not return a latitude and longitude. In some cases it will return null. This is the reason I use provider way of fetching the location. For my app I do not require continuos updates of location. It is fetched only on some particular event. For this, I use requestLocationUpdates and once I get the location I remove the listener.
sunil
Ah, in that case you'll have to implement a timeout and just take network location if GPS hasn't come down in that time. That's how the people in [this SO question](http://stackoverflow.com/questions/2919358/android-get-location-from-best-provider-available) solved it.
Neil Traft

related questions