【问题标题】:cannot resolve symbol getFusedLocationProviderClient and LocationCallback无法解析符号 getFusedLocationProviderClient 和 LocationCallback
【发布时间】:2019-01-14 07:22:10
【问题描述】:

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

getFusedLocationProviderClient(this).requestLocationUpdates(mLocationRequest, new LocationCallback() {
                    @Override
                    public void onLocationResult(LocationResult locationResult) {
                        // do work here
                        onLocationChanged(locationResult.getLastLocation());
                    }
                },
                Looper.myLooper());

我已将dependencies 包含在我的应用程序build.gradle

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

我应该导入或包含什么才能使此方法符号起作用? 请帮忙

【问题讨论】:

  • 你用的是什么版本的gradle?如果是旧版本,则需要将implementation 更改为compile
  • @HedeH gradle 版本 4.4
  • 如果你没有静态导入然后:getFusedLocationProviderClient(this) >>> LocationServices.getFusedLocationProviderClient(getContext())
  • 确保您已将类路径添加到根项目dependencies { classpath 'com.google.gms:google-services:4.0.1' // google-services plugin },并且不要忘记清理和构建

标签: android google-maps-android-api-2 android-gps


【解决方案1】:

替换

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

getFusedLocationProviderClient(this)

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)

更新

使用此代码请求位置更新..,检查此Github Repo 并附上完整示例。

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;
            }
        }
    });
}

【讨论】:

  • 这是我在尝试重建 error: cannot find symbol method getFusedLocationProviderClient(findme)error: cannot find symbol class LocationCallbackwhere findme 是我的活动时遇到的错误
猜你喜欢
  • 2021-02-04
  • 2018-11-11
  • 1970-01-01
  • 2023-03-17
  • 1970-01-01
  • 2019-02-14
  • 2020-06-17
  • 2017-01-16
  • 1970-01-01
相关资源
最近更新 更多