I'm trying to get the most accurate location.
till now I'v used successfully LocationClient as in Google docs: http://developer.android.com/training/location/retrieve-current.html
But now I have discovered that this class is old: http://developer.android.com/reference/com/google/android/gms/location/package-summary.html
"This class is deprecated. Use LocationServices".
However in the examples of LocationServices even in this site: Android LocationClient class is deprecated but used in documentation
It seem we should use an import: com.google.android.gms.location.LocationServices However I can't import this class..
Any Ideas ?
Edit:
I have found in this page: h2ttps://github.com/M66B/XPrivacy/issues/1774
with seemingly similar problem, a speculation that: "Seems to be part of Play services 5.0."
I wonder if that is the case -- has anybody tried this ?
Make sure you have the following item in your gradle dependency:
dependencies {
implementation 'com.google.android.gms:play-services-location:7.+'
}
Please Don't use compile 'com.google.android.gms:play-services:9.0.1'
In versions of Google Play services prior to 6.5, you had to compile the entire package of APIs into your app. In some cases, doing so made it more difficult to keep the number of methods in your app (including framework APIs, library methods, and your own code) under the 65,536 limit.
From version 6.5, you can instead selectively compile Google Play service APIs into your app. For example, in your case you just needed this.
compile 'com.google.android.gms:play-services-maps:9.0.1'
compile 'com.google.android.gms:play-services-location:9.0.1'
To use fused api to get last location you must have compile 'com.google.android.gms:play-services-location:9.0.1'
.
Hope it helps
Below might help,
It should import the respective package in project.
I encountered the same issue, and solved it by updating Google Play Services from 17 to the latest (currently 22) in Android SDK Manager, as mentioned in the comments.
To do that in Eclipse:
LocationClient is deprecated. You have to use GoogleApiclient
, like this:
1: Declare a GoogleApiClient variable
private GoogleApiClient mGoogleApiClient;
2: Instantiate
mGoogleApiClient = new GoogleApiClient.Builder(mThisActivity)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
3: Implement Callback
public class YourClass extends BaseFragment implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener, LocationListener {
@Override
public void onConnectionFailed(ConnectionResult result) {
// your code goes here
}
@Override
public void onConnected(Bundle connectionHint) {
//your code goes here
}
@Override
public void onConnectionSuspended(int cause) {
//your code goes here
}
}
4: Start to get Location Updates:
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);
5: Remove Location Updates:
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
6: Get Last Known Location:
private Location mCurrentLocation;
mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
As of 2019 or later, the best answer is outdated and will not work.
Instead, add:
implementation 'com.google.android.gms:play-services-location:17.0.0'
to dependencies
in build.gradle
.
Follow this steps if you are using eclipse :-
It's a similar question to issue and answer given here :
In Android Studio:
Right click on your projects "app" folder and click on -> module settings
Click on the "dependencies" tab
Click on the + sign to add a new dependency and select "Library Dependency" Look for the library you need and add it
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.