xamarin.essentials.geolocation 返回空位置

问题描述

我正在我的 Xamarin android 应用程序上实现谷歌地图。这是我在 OnCreate() 中调用方法获取当前位置。

public async Task<LatLng> getCurrentLocation()
        {
            var location = await Geolocation.GetLocationAsync(new GeolocationRequest
            {
                DesiredAccuracy = GeolocationAccuracy.Medium,Timeout = TimeSpan.FromSeconds(10)
            });
            if (location != null)
            {

                var location_current = new LatLng(location.Latitude,location.Longitude);
                return location_current;
                
            }
            return new LatLng(63,42);
        }

有时它会返回正确的位置,但有时它会返回 null,因为它从未被调用过。可能是什么问题?

我试过了:

  1. 调试并确保每次都命中此代码,即使它返回 null。
  2. 确保这不是位置权限问题,因为我已经在 AndroidManifest.xml 以及 OnRequestPermissionResult() 方法中包含了所有相关权限

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)