微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决有没有办法 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 举报,一经查实,本站将立刻删除。