android - FusedLocationProviderClient 没有方位数据

标签 android location fusedlocationproviderclient

我在android中使用FusedLocationProviderClient类来获取用户的最后位置。一切都很好,我可以获得纬度和经度,但是 task.getResult().getBearing() 返回 0.0task.getResult().hasBearing() 返回 false。如何获取用户的方位信息?

我在 AndroidManifest 文件中和运行时都获得了 ACCESS_FINE_LOCATIONACCESS_COARSE_LOCATION 权限。

这是与我正在使用的用户位置相关的代码段:

@SuppressLint("MissingPermission")
private void getLastLocation() {
    fusedLocationClient
            .getLastLocation()
            .addOnCompleteListener(this, new OnCompleteListener<Location>() {
                @Override
                public void onComplete(@NonNull Task<Location> task) {
                    if (task.isSuccessful() && task.getResult() != null) {
                        onLocationChange(task.getResult());
                        Log.i(TAG, "lat " + task.getResult().getLatitude() + " lng " + task.getResult().getLongitude());
                        Log.i(TAG, "bearing? " + task.getResult().hasBearing());
                    } else {
                        Toast.makeText(MainActivity.this, "Location Not Found!", Toast.LENGTH_SHORT).show();
                    }
                }
            });

}

private void onLocationChange(Location location) {
    //do something
}

最佳答案

根据documentation

If this location does not have a bearing then 0.0 is returned.

关于android - FusedLocationProviderClient 没有方位数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51628283/

相关文章:

ios - 是否可以将 google places 限制为餐厅或咖啡店等一两个地方?

sorting - 如何根据函数返回的值在dart中对List进行排序

android - FusedLocationClient 在请求后不会停止搜索 gps

android - 应用程序中未显示已编辑的 Vector Drawable

java - 如何设置一个仅在应用程序关闭时调用的方法?

android - Handlers、MessageQueue、Looper,它们都运行在 UI 线程上吗?

ios - 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[MKUserLocation标记]:

android - 如何将图像转换为通知图标以在 Android 应用程序中使用?

android - FusedLocationProviderClient 始终为 null,locationAvailability 通常为 false

android - FusedLocationClient 未调用 onLocationResult