无法解析符号 getFusedLocationProviderClient 和 LocationCallback

Posted

技术标签:

【中文标题】无法解析符号 getFusedLocationProviderClient 和 LocationCallback【英文标题】:cannot resolve symbol getFusedLocationProviderClient and 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 ,并且不要忘记清理和构建 【参考方案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;
            
        
    );

看看这个article from Android Developers Blog:

【讨论】:

这是我在尝试重建 error: cannot find symbol method getFusedLocationProviderClient(findme)error: cannot find symbol class LocationCallbackwhere findme 是我的活动时遇到的错误

以上是关于无法解析符号 getFusedLocationProviderClient 和 LocationCallback的主要内容,如果未能解决你的问题,请参考以下文章

ACE编程,无法解析外部符号

error LNK2001: 无法解析的外部符号

error LNK2019: 无法解析的外部符号

C++无法解析的外部符号

error LNK2019: 无法解析的外部符号

vs2017,vs2019,无法解析的外部符号 sscanf,无法解析的外部符号 _snprintf,无法解析的外部符号 _vsnprintf,无法解析的外部符号 __iob_func