delight-im / Android-SimpleLocation

Utility class for easy access to the device location on Android
Apache License 2.0
200 stars 75 forks source link

auto select network or gps ? #10

Closed moshlp closed 8 years ago

moshlp commented 8 years ago

I am developing an app where the location must be invisible to the user. The idea is that if the user has GPS enabled, the coordinates go with FINE, and if only network enabled, the coordinates are sent with COARSE, is there any way to achieve that? thanks

ocram commented 8 years ago

Thanks for your question!

Yes, absolutely!

Just add both permissions (fine and coarse), as shown in the README and do not set the second parameter of the constructor to true. That's all. It will pick whichever method is available.

The second parameter of the constructor is called requireFine and must thus be false (or left out) in your case: https://github.com/delight-im/Android-SimpleLocation/blob/2646e1b7ee811d64395b3d09cbb08be1c5e0ee85/Source/library/src/main/java/im/delight/android/location/SimpleLocation.java#L149

If none is available, it won't be able to do anything, of course. That's where the example suggests to open the device's settings for the user.

moshlp commented 8 years ago

thanks, I appreciate your answer. There's a detail I forgot to mention: what happens if Location service is disabled? in order to use SimpleLocation, should Location be always enabled? If I apply your previous answer, it runs great but with the Location activated, but is there any way to get the coordinates with Location disabled? To be clearer on my app: if the person has Location disabled, I should be able to send the coordinates taken from network or wifi; if enabled then GPS. I hope I'm clearer this time,

thanks,

ocram commented 8 years ago

what happens if Location service is disabled?

The sample application and the README suggest to open the location settings of the device for the user in this situation. Otherwise, you decide what happens.

in order to use SimpleLocation, should Location be always enabled?

Definitely! No matter if you use this library, some other library, or no library at all -- you have to enable location (either network/coarse or GPS/fine or both) in order to retrieve some information.

but is there any way to get the coordinates with Location disabled?

No. This is because the operating system (correctly) respects the user's choice about disabling location access.

if the person has Location disabled, I should be able to send the coordinates taken from network or wifi; if enabled then GPS.

You can't. As I've written above, you get the available location automatically if one or both location providers are enabled. But if none is enabled, you simply can't do this.

All you can do in that situation is trying some other means of locating the user, e.g. retrieving an approximate location from the user's IP address.

Again, this is not a restriction of this library but an intended safeguard on part of the operating system.