Android 无法解析符号getFusedLocationProviderClient和LocationCallback

Android 无法解析符号getFusedLocationProviderClient和LocationCallback,android,google-maps-android-api-2,android-gps,Android,Google Maps Android Api 2,Android Gps,我是android编程的新手。我正在尝试访问某个位置,但此代码有错误 getFusedLocationProviderClient(this).requestLocationUpdates(mLocationRequest, new LocationCallback() { @Override public void onLocationResult(LocationResult locationResult) {

我是android编程的新手。我正在尝试访问某个位置,但此代码有错误

getFusedLocationProviderClient(this).requestLocationUpdates(mLocationRequest, new LocationCallback() {
                    @Override
                    public void onLocationResult(LocationResult locationResult) {
                        // do work here
                        onLocationChanged(locationResult.getLastLocation());
                    }
                },
                Looper.myLooper());
我已经在我的应用程序
build.gradle

implementation 'com.google.android.gms:play-services-maps:15.0.1'
implementation 'com.google.android.gms:play-services:12.0.1'

我应该导入或包含什么才能使此方法有效请帮助更换

implementation 'com.google.android.gms:play-services-maps:15.0.1'
implementation 'com.google.android.gms:play-services-location:15.0.1'

LocationServices.getFusedLocationProviderClient(this)
更新

使用此代码请求位置更新。请用完整的示例检查此代码。

protected void createLocationRequest() {
    locationRequest = LocationRequest.create();
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    locationRequest.setInterval(30 * 1000);
    locationRequest.setFastestInterval(5 * 1000);
    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
            .addLocationRequest(locationRequest);
    //**************************
    builder.setAlwaysShow(true); //this is the key ingredient
    //**************************
    PendingResult<LocationSettingsResult> result =
            LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build());
    result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
        @Override
        public void onResult(@NonNull LocationSettingsResult result) {
            final Status status = result.getStatus();
            final LocationSettingsStates state = result.getLocationSettingsStates();
            switch (status.getStatusCode()) {
                case LocationSettingsStatusCodes.SUCCESS:
                    // All mLocation settings are satisfied. The client can initialize mLocation
                    // requests here.
                    enableMyLocation();
                    break;
                case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                    // Location settings are not satisfied. But could be fixed by showing the user
                    // a dialog.
                    try {
                        // Show the dialog by calling startResolutionForResult(),
                        // and check the result in onActivityResult().
                        status.startResolutionForResult(MapsActivity.this, REQUEST_ACCESS_FINE_LOCATION_PERMISSION);
                    } catch (IntentSender.SendIntentException e) {
                        // Ignore the error.
                    }
                    break;
                case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                    // Location settings are not satisfied. However, we have no way to fix the
                    // settings so we won't show the dialog.
                    break;
            }
        }
    });
}
受保护的void createLocationRequest(){
locationRequest=locationRequest.create();
locationRequest.setPriority(locationRequest.PRIORITY\u高精度);
位置请求设置间隔(30*1000);
locationRequest.SetFastTestInterval(5*1000);
LocationSettingsRequest.Builder=新建LocationSettingsRequest.Builder()
.addLocationRequest(locationRequest);
//**************************
builder.setAlwaysShow(true);//这是关键要素
//**************************
Pendingreult结果=
LocationServices.SettingsApi.checkLocationSettings(GoogleAppClient,builder.build());
result.setResultCallback(新的ResultCallback(){
@凌驾
public void onResult(@NonNull LocationSettingsResult result){
最终状态状态=result.getStatus();
最终位置SettingsStates状态=结果。getLocationSettingsStates();
开关(status.getStatusCode()){
案例位置设置StatusCodes.SUCCESS:
//满足所有mLocation设置。客户端可以初始化mLocation
//请求在这里。
enableMyLocation();
打破
案例位置设置StatusCodes.RESOLUTION_要求:
//不满足位置设置。但可以通过显示用户来修复
//对话。
试一试{
//通过调用startResolutionForResult()显示对话框,
//并在onActivityResult()中检查结果。
status.startResolutionForResult(mapsacativity.this,请求\访问\确定\位置\权限);
}catch(IntentSender.sendtintentexe){
//忽略错误。
}
打破
案例位置设置StatusCodes.SETTINGS\u CHANGE\u不可用:
//位置设置不满意。但是,我们无法修复此问题
//设置,这样我们就不会显示对话框。
打破
}
}
});
}
  • 看看这个:

以下是我在尝试重建时遇到的错误
错误:找不到符号方法getFusedLocationProviderClient(findme)
错误:找不到符号类LocationCallback
findme是我的活动的位置您使用的是哪个版本的gradle?如果它是旧版本,您需要将
实现
更改为
编译
@HedeH gradle verson 4.4如果您没有静态导入,那么:
getFusedLocationProviderClient(this)
>>
LocationServices.getFusedLocationProviderClient(getContext())
确保已将类路径添加到根项目
依赖项{classpath'com.google.gms:google services:4.0.1'//google services plugin}
,并且不要忘记清理和构建。
protected void createLocationRequest() {
    locationRequest = LocationRequest.create();
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    locationRequest.setInterval(30 * 1000);
    locationRequest.setFastestInterval(5 * 1000);
    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
            .addLocationRequest(locationRequest);
    //**************************
    builder.setAlwaysShow(true); //this is the key ingredient
    //**************************
    PendingResult<LocationSettingsResult> result =
            LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build());
    result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
        @Override
        public void onResult(@NonNull LocationSettingsResult result) {
            final Status status = result.getStatus();
            final LocationSettingsStates state = result.getLocationSettingsStates();
            switch (status.getStatusCode()) {
                case LocationSettingsStatusCodes.SUCCESS:
                    // All mLocation settings are satisfied. The client can initialize mLocation
                    // requests here.
                    enableMyLocation();
                    break;
                case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                    // Location settings are not satisfied. But could be fixed by showing the user
                    // a dialog.
                    try {
                        // Show the dialog by calling startResolutionForResult(),
                        // and check the result in onActivityResult().
                        status.startResolutionForResult(MapsActivity.this, REQUEST_ACCESS_FINE_LOCATION_PERMISSION);
                    } catch (IntentSender.SendIntentException e) {
                        // Ignore the error.
                    }
                    break;
                case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                    // Location settings are not satisfied. However, we have no way to fix the
                    // settings so we won't show the dialog.
                    break;
            }
        }
    });
}