有没有办法 Geolocation.GetLastKnownLocationAsync();不要在使用 xamarin 的 android 项目中返回 null

问题描述

问题出在android项目上。我在iOS项目中没有这样的问题。

每次来自 GPS 的变量返回给我时:null

我在 stackoverflow 上读了一篇用户给我的文章https://github.com/xamarin/Essentials/issues/519,但我不是很清楚如何解决问题。

这是我的 GPS 按钮代码

 async Task ButtonClickedGPS()
    {
        
        var status = await Permissions.CheckStatusAsync<Permissions.LocationWhenInUse>();

        if (status == PermissionStatus.Denied && status == PermissionStatus.disabled)
        {
            _ = await Permissions.RequestAsync<Permissions.LocationWhenInUse>();
        }
        else
        {
            try
            {
                var location = await Geolocation.GetLastKNownLocationAsync();

                if (location != null)
                {
                    WeatherBindingData weatherBindingData = await _restServiceForecast.GetWeatherDataForecast(GenerateRequestUriGPS(Constants.OpenWeatherMapEndpoint,location),GenerateRequestUriForecast(ConstantsForecast.OpenWeatherMapEndpoint2,_cityEntry),GenerateRequestUriAir(ConstantsAir.OpenWeatherMapEndpoint3,location));
                   
                    var CityName = weatherBindingData.WeatherDataCurrent.Title.ToString();
                    
                    _cityEntry.Text = CityName;

                   await displayHourlyForecast();
                }
            }
            catch (FeatureNotSupportedException fnsEx)
            {
                // Handle not supported on device exception
            }
            catch (FeatureNotEnabledException fneEx)
            {
                // Handle not enabled on device exception
            }
            catch (PermissionException pEx)
            {
                // Handle permission exception
            }
            catch (Exception ex)
            {
                // Unable to get location
            }
        }

按钮的逻辑很简单:我从手机的 GPS 获取坐标,将它们传递给 API,然后它将定居点的名称返回到它的坐标,然后我将名称附加到搜索中。 我的 iOS 项目没有任何问题,但 Android var location = await Geolocation.GetLastKNownLocationAsync(); 即将推出:null

我该如何解决这个问题?从我作为超链接附加的主题,我想我明白在启动应用程序时应该关闭用户的 GPS,但这对我不起作用

解决方法

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

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

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