找不到com.google.android.gms.location.LocationClient类(android)

Jer*_*Sun 30 android location

我从http://developer.android.com/training/location/retrieve-current.html下载了一个演示项目,我想我没有丢失任何步骤; 但我找不到哪个jar文件包含"com.google.android.gms.location.LocationClient.class"文件

我们发现所有"google-play-services.jar"和"maps.jar"以及"android.jar(所有版本)"都不包含"LocationClient.class"?

Ate*_*etc 25

添加到Gradle文件(xyz - Google Play服务的实际版本):

compile 'com.google.android.gms:play-services-location:x.y.z'
Run Code Online (Sandbox Code Playgroud)


小智 16

最后一个GPS lib存在一些问题.您必须使用比最新版本(6. +)更旧的版本.尝试使用旧版本.我没有看到文档中有任何内容被弃用或遗漏了LocationClient.class.

compile 'com.google.android.gms:play-services:5.+'
Run Code Online (Sandbox Code Playgroud)

  • 这可行,因为在5. +现在已弃用的LocationClient仍然包括在6.5.+他们用GoogleApiClient替换它,更多信息在这里http://stackoverflow.com/questions/24611977/android-locationclient-class-is-弃用,但使用的功能于文档 (3认同)

小智 13

不推荐使用LocationClient.你必须使用GoogleApiclient,像这样:

1:声明GoogleApiClient变量

private GoogleApiClient mGoogleApiClient;
Run Code Online (Sandbox Code Playgroud)

2:实例化

mGoogleApiClient = new GoogleApiClient.Builder(mThisActivity)
     .addApi(LocationServices.API)
     .addConnectionCallbacks(this)
     .addOnConnectionFailedListener(this)
     .build();
Run Code Online (Sandbox Code Playgroud)

3:实现回调

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       
    }
}
Run Code Online (Sandbox Code Playgroud)

4:开始获取位置更新:

LocationServices.FusedLocationApi.requestLocationUpdates(
            mGoogleApiClient, mLocationRequest, this);
Run Code Online (Sandbox Code Playgroud)

5:删除位置更新:

LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
Run Code Online (Sandbox Code Playgroud)

6:获取最后一个位置:

private Location mCurrentLocation;

mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
Run Code Online (Sandbox Code Playgroud)


Rik*_*ati 1

您需要更新 Google Play Service SDK,然后使用更新的库。