Android Location: SO answers don't agree with Android Dev site - android

I'm trying to build an app using the GoogleMaps API and the user's location (once-off) and trying to find out how best to approach this but it seems like the approach the Stack Overflow answers take is very different to the approach the Android Developer site takes.
Basically the SO answers suggest subscribing to service that gives you location updates which you handle in a callback, whether you want the location once (like me) or actually plan to use the updated positions. (Post 1, Post 2, both highly up-voted)
However the Android dev site says you should connect a LocationClient and then just call getLastLocation() as you need it, which removes a bunch of the switching between sync and async coding elements.
So, I'm still confused as to what the "best-practice" way of doing this is and to why the Android dev site doesn't agree with the high-scoring answers here.

Both the posts you've linked to are quite old (3 and 4 years old).
Android has come a long way in that time, and now we have an excellent Location API as part of the Google Play Services that you should use to get the user's location. This would be the current best practice.

Android provides 2 ways to get GPS location. LocationManager and LocationClient. LocationManager has a request callback type and a get last known location. LocationManager has inherent issues. Some phones dont report back the callback at all, nor do they return back the Location from get last known location. LocationManager also uses a lot more battery.
LocationClient is the new and best way currently. They also use much lesser battery.

Related

Google PlaceDetectionClient vs FusedLocationProviderClient

My Android app requires the user to enter the current location either by manually searching or by pressing a button to automatically detect the location. I've done the search part using PlaceAutocompleteFragment, but for the detection part I'm confused between the above mentioned classes. To my knowledge, PlaceDetectionClient provides details about the actual building, like a business, and FusedLocationProviderClient uses various sensors to detect the location. Aside from the building detail thing, what's the difference between the two? Which one is more reliable and easier to implement?
It depends on what do you need. If you need user location in your app, better way is to rely on latitude and longitude, so I believe it is better use from FusedLocation which provides exactly what you need.
PlaceDetectionClient is API to detect places where user is nearby i.e check-in feature in current place. I am not sure it is what you really need.

What is the best way to get latitude and longitude from GPS in Android?

Just simple as that.I dont want to find new coordinates every 10 minutes or meters.I just want to click a button and get my coordinates for this exact moment/place where i am.Every tutorial i've seen its old.New android versions require permissions to use GPS or Network.I am trying to implement those but nothing seems to work.
Any solutions?
Thanks in advance
I just want to click a button and get my coordinates for this exact moment/place where i am
That's not possible in general. The device may not know the user's "coordinates for this exact moment/place". You are welcome to call getLastKnownLocation() on LocationManager, or perform equivalent sorts of lookups with Play Services' fused location provider, but they may return null. Usually, to find out where the user is, you request location updates, then react when you get a location fix.
Every tutorial i've seen its old
These sample apps (for the fused location provider) and this sample app (for LocationManager), from my book, should be up to date.
New android versions require permissions to use GPS or Network
All Android versions require permissions to determine the user's location, for blindingly obvious privacy reasons. This has been the case since before Android 1.0, and it should remain the case for the foreseeable future.
I am trying to implement those but nothing seems to work
Then ask a fresh Stack Overflow question, where you provide a minimal, complete, and verifiable example demonstrating your problems.

GoogleApiClient Vs LocationClient to get accurate location in Android

I am trying to get an accurate location for my emergency alert app. I am already using the locationManager to get the location but fetching location with this technique takes about 30seconds I am looking for faster & accurate way.
The main goal here is to get faster/accurate location (Indoor or outdoor).
I have decided to use GooglePlayservices API for this purpose. I figured out there are two options LocationClient and GoogleApiClient?
I am not sure what is best here? Can somebody tell me what is the difference with both or is there a better option?
Let me know!
Thanks!
The GoogleApiClient location client is good if you need a fused high accuracy location.
With fused I mean a location calculated using Wifi, GPS and so on.
With the location client, instead, you have to select a provider. You can even use a criteria, but in this case I think the information is not "fused". In your case I think the best choice is to use the GoogleApiClient location client and you can use getLastLocation().
I would recommend to use GooglePlayServices, not only by the reasons #gresywolf82 hasstated, but also because it is most stable way of getting Location data.
In my experience I have encountered that some Android Devices may have issues obtaining location from the SDK's locationManager, this is due to probable bugs in the SDK, you can check an issue in the Android Open Source Project - Issue Tracker regarding this problem with the locationManager here
I have found the previous problem myself while using the locationManager and had absolutely no issues when switching to Google Play Services API.
SO in conclusion, not just for more precision but also for better stability within various devices I definitely recommend using Google Play Services API.
Both LocationClient and GoogleApiClient provide fused locations.
It is the LocationManager where you need to specify your own providers, this is a low level access if you want to roll your own.
As far as I can see, the GoogleApiClient is an aggregate of LocationClient and other GoogleApis (someone pls. correct if I'm mistaken). If you only wish to target LocationClient you may do so without any Activity compliance.
On the other hand, it would appear that GoogleApiClient requires tight coupling with the Activity life cycle, needing to be disconnected onStop and connected onStart. The trouble I have with such asynchronous calls is trying to make sure the API is up and ready by the time I need it. Therefore I usually go with LocationClient and keep it running outside of Activity lifecycles.
This works, but if you have issues with power consumption you may want to disconnect when app is in background, and reconnect when app comes into foreground.
Do not use GoogleApiClient. It is old and problematic. Use the new API. Please read the following Android newsletter link for the details :
https://android-developers.googleblog.com/2017/06/reduce-friction-with-new-location-apis.html

Can using two different LocationListener's cause problems?

I have two different classes in my application that implement LocationListener. One of them is only used as needed to get the user's current location, while the other is constantly run in the background to keep data on the user's speed.
On my test device, this causes no problems. Some other people testing however have said that their device "locks" (I have not seen this occur, so I'm unsure what they truly mean by this) when trying to acquire the location.
Is it possible that this is just their phone having a weak GPS fix, or could this be some weird hardware issue that occurs when two different threads are trying to gain the location via GPS? If so, what's the best way to work around this? Thanks.
I would suggest you use the new fused provider that is part of Google Play Services. I would not set two LocationListeners. Since you will be constantly tracking the user's location, by using the new LocationClient you can use its getLastLocation() to retrieve the most recent best, cached location the device has, which is really likely to be one from the constant updates you are already retrieving. The fused provider will also determine which one is the best provider to take the location from, you do not need to worry about that logic.

LocationClient vs LocationManager

What is the difference between LocationClient and LocationManager.
What is the pros and cons between them (like battery, accuracy)?
Which is better to use?
Location Manager was introduced in Android SDK and can be used as a feature of android.
Location Client is something that's part of Google Play SDK and is introduced in the recent Google IO 2013.
One can understand that since Location Client is the latest, it is more efficient in getting the location with minimal energy(battery drain) with greater accuracy.
UPDATE: LocationClient is deprecated. You have to use GoogleApiClient. An example of it can be found here.
Google Play Services Team has cleaned up their code and moved LocationClient functionality into GoogleApiClient.
Tutorial for the same is available in
http://developer.android.com/training/location/retrieve-current.html
On following link you can find IO talk about this subject
http://www.youtube.com/watch?v=Bte_GHuxUGc
UPDATE AGAIN
GoogleApiClient has been deprecated again, you have to use GoogleApi based APIs instead.
I have been developing a location based application in android and I seriously NOT recommend using the LOCATION CLIENT in any case. Reasons :
The location update behavior is very abnormal and wont work as you expect. i.e. The location updates get stuck when switching networks. (It keeps giving you some old location)
The location client wont work on modified android versions of the android OS, as it requires Google play services.
With my experience, Location Client might be good on the battery of the phone but it won't be good with giving you timely accurate location updates.
I recommend good old Location Manager as I don't find location client reliable at all.
P.S. : There is no point of saving battery if you are not even getting your current location in a location based application.
EDIT:
If you know the implementation of LocationManager and LocationClient (both are available in documentation), you can create your own LocationClient-like wrapper (with callbacks and stuff), which will be working on LocationManager but with custom tweakable properties.
EDIT 2:
Please find the LocationManager Wrapper class here, which provides timely location updates:
https://github.com/rahulsh12/LocationManagerWrapper
I have worked on a tracking app and my experience is that LocationManager is better than LocationClient. LocationClient does not provide any way to specify that you want location updates from GPS only. All it allows is to specify "high accuracy". This works for most part but every now and then you get a location update which is hundreds of meters off BUT with a specified accuracy of a few meters. There is no way to know you got an unusable sample. With LocationManager if you specify GPS_PROVIDER you can be assured that you are never going to get wildly inaccurate samples. Working well for us.
Coming from someone who switched over to Google Play Services a while ago, i can give you some experiences:
I have an app, about 2,5 years old, that uses location services extensively. From the outset, of course, we used the LocationManager since that's what was available on the Android platform.
We had a pretty bad experience with Location Services on Android compared to IOS. It was buggy, unreliable, and gave less precise locations than than our IOS app, plus that it drained more battery. It was a drag.
Therefor, when Google unveiled the new API in june this summer, we jumped at it. It is way better. A couple of things:
It is quicker and more reliable.
It is less buggy. As an example, in the old API we could sometimes get an "old" fix with a new timestamp. This never happens anymore. There's more but it would be an even more lengthy post.
It definitely drains less battery. For example, when you had a map view open, the GPS ran all the time, and the GPS icon was visible. This is not the case with the new one. This made users wonder what was going on. This is nolonger as big an issue.
So, when it comes to the location output and work, everything is better. But there are some downsides:
You have to have Google Play Services installed, meaning it wont work on any "non-google-approved" phone models, and in some instances you'll have to tell users they need to install it.
The API itself is more complex IMO, in part due to point 1. In addition to the "regular" callbacks i.e. waiting for location fixes etc. You now have a process that takes part before you can get started where you have to check that playservices is available, and "connect" the locationclient. This is extra code and a bit more complex to grasp. More faulty conditions to take into account in the code too (if you can be bothered...)
Google play services itself requires at least 2.2 so it won't work for older devices than that. We had to tell some clients they had to upgrade...
Hope this helps.

Categories

Resources