27

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 ?

Community
  • 1
  • 1
d_e
  • 435
  • 1
  • 6
  • 15
  • Are you sure that you are using the latest Play Services library project? – CommonsWare Aug 18 '14 at 19:28
  • @CommonsWare According to the SDK Manager I'm using Rev: 17. it appear to be the latest – d_e Aug 18 '14 at 19:34
  • That's what you have *downloaded*. Did you copy that specific downloaded version somewhere, then attach that copy to your app project? If your app project is using some older edition of the library project, that would explain the missing class. – CommonsWare Aug 18 '14 at 19:37
  • Revision 19 is the latest. I'm not sure if it makes any difference – NickT Aug 18 '14 at 19:37
  • @CommonsWare I don't recall to do any updates in what i have downloaded. However, how can I check the version of the version my app actually uses ? – d_e Aug 18 '14 at 19:40
  • There's no good easy way to do that, at least that I can think of. You can see where your copy is, but as to what version the copy represents, I couldn't tell you. This gets a *lot* easier with Android Studio and Gradle for Android. – CommonsWare Aug 18 '14 at 19:44
  • In C:\YourSdkLocation\extras\google\google_play_services you will find the file source.properties which is human readable, the version is listed there. – NickT Aug 18 '14 at 19:49
  • @NickT . Thanks. it indeed read 17.0.0, However API is 19. Are you sure Rev 19 is the lastest ? – d_e Aug 18 '14 at 19:56
  • Yes 19 is the latest, you can check this by running your SDK manager. I'm not sure that there is any correlation between the Play Services lib rev number and compatiblity with the SDK's API revision. Google don't provide much info as far as I can see. – NickT Aug 18 '14 at 20:01
  • I cant see..SDK manager reads rev 17 for me. Is Rev 19 is the latest – d_e Aug 18 '14 at 20:02
  • I downloaded rev 19 on the 6th August. (My SDK Manager checks for updates and asks if I want to download them, whch I did) – NickT Aug 18 '14 at 20:10
  • As it happens I have a copy of rev 17 GPServices.jar and one of rev 19, I can confirm that com/google/android/gms/location/LocationServices.class is in 19 BUT NOT IN 17, so you will need to get 19 somehow. – NickT Aug 18 '14 at 20:22
  • @NickT Thanks for you check you have done for me. How can I tell SDK Manager to check for updated(as it appear to do it already on every startup)... I can't find the option... – d_e Aug 18 '14 at 20:29

8 Answers8

50

Make sure you have the following item in your gradle dependency:

dependencies {
    implementation 'com.google.android.gms:play-services-location:7.+'
}
Aaron Lelevier
  • 19,850
  • 11
  • 76
  • 111
k.chao.0424
  • 1,191
  • 10
  • 11
19

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'.

For more Information

Hope it helps

Karue Benson Karue
  • 957
  • 12
  • 26
  • 1
    If you compile 'com.google.android.gms:play-services:x.x.x you get a warning message. Use the elements you actually need. By the way, compile has now been replaced with implementation – PhilipS Apr 07 '18 at 12:14
  • but one question, if I add the version with "play services location" instead of "play service" as you told to do, should I delete "compile 'com.google.android.gms:play-services-maps:9.0.1' " ? (or is it possible that this still be useful for something else?) In one word : is the library you told to not use completely deprecated ? – Henley n Mar 01 '19 at 23:21
3

Below might help,

  1. You should have play service latest revision 22 downloaded.
  2. Add this line in dependencies compile 'com.google.android.gms:play-services:+'

It should import the respective package in project.

Vinayak
  • 39
  • 3
  • what does mean the "+" at the end ? it means "give me the lastest version of this lib"? – Henley n Mar 02 '19 at 00:42
  • 1
    Yes, that's correct. Although its not recommended to use '+' as then it will keep on getting whatever new lib version available and some release might have unintentional bugs which you don't want to be part of your app. So if you are sure on version best thing is to use fix version. – Vinayak May 16 '19 at 04:25
2

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:

  • Delete google-play-services_lib project from your Eclipse
  • Follow Step 3 of Adding SDK Packages. When you launch Android SDK Manager, it should say "Update available: Rev 22" for Google Play Services. Check it and click "Install N packages" to update it.
  • Follow Setting Up Google Play Services to import google-play-services_lib project again.
Hiroshi Ichikawa
  • 616
  • 9
  • 10
  • Does this mean you also have to have the latest Google Play Services installed on every phone where you want to start the app? – oliver May 03 '15 at 22:01
  • Yes. Usually the app asks you to install the latest Google Play Services when it's older than required. I'm not sure if the prompt is automatically done by Google Play Services, or you need extra coding to make it happen. – Hiroshi Ichikawa Jun 01 '15 at 01:21
  • Does that also require that a Google account is registered in the phone? Or does the update also work without such an account? – oliver Jun 01 '15 at 08:10
2

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);
Naveen Kumar M
  • 7,497
  • 7
  • 60
  • 74
2

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.

Dan Bray
  • 7,242
  • 3
  • 52
  • 70
0

Follow this steps if you are using eclipse :-

  1. Update Google play library from SDK Manger.
  2. Delete old google-play-services_lib from workspace and remove all dependency.
  3. Import update google-play-services_lib and make sure copy to workspace option is selected

copy to work space

  1. Add google-play-services_lib to build path of project it should be showing relative path enter image description here

  2. If step it is not working restart eclipse .

Hitesh Sahu
  • 41,955
  • 17
  • 205
  • 154
0

It's a similar question to issue and answer given here:

In Android Studio:

  1. Right click on your projects "app" folder and click on -> module settings

  2. Click on the "dependencies" tab

  3. Click on the + sign to add a new dependency and select "Library Dependency" Look for the library you need and add it

Community
  • 1
  • 1
ransh
  • 1,589
  • 4
  • 30
  • 56