Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/198.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 是否有一种方法用于地理定位。GetLastKnownLocationAsync();在使用xamarin的android项目中不返回null_C#_Android_Xamarin - Fatal编程技术网

C# 是否有一种方法用于地理定位。GetLastKnownLocationAsync();在使用xamarin的android项目中不返回null

C# 是否有一种方法用于地理定位。GetLastKnownLocationAsync();在使用xamarin的android项目中不返回null,c#,android,xamarin,C#,Android,Xamarin,问题在于android项目。我在iOS项目中没有这样的问题 每次来自GPS的变量返回我时:null 我读了一篇用户给我的关于stackoverflow的文章,但我不太清楚如何解决这个问题 这是我在GPS按钮上的代码: async Task ButtonClickedGPS() { var status = await Permissions.CheckStatusAsync<Permissions.LocationWhenInUse>();

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

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

我读了一篇用户给我的关于stackoverflow的文章,但我不太清楚如何解决这个问题

这是我在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
            }
        }
异步任务按钮ClikedGPS()
{
var status=等待权限。CheckStatusAsync();
if(状态==PermissionStatus.Denied&&status==PermissionStatus.Disabled)
{
_=等待权限。RequestAsync();
}
其他的
{
尝试
{
var location=等待地理位置。GetLastKnownLocationAsync();
如果(位置!=null)
{
WeatherBindingData WeatherBindingData=await _restServiceForecast.GetWeatherDataForecast(GenerateRequestUriGPS(Constants.OpenWeatherMapEndpoint,位置),GenerateRequestUriForecast(ConstantsForecast.OpenWeatherMapEndpoint2,_cityEntry),GenerateRequestUriAir(ConstantsAir.OpenWeatherMapEndpoint3,位置));
var CityName=weatherBindingData.WeatherDataCurrent.Title.ToString();
_cityEntry.Text=CityName;
等待DisplayHourlyForecast();
}
}
捕获(功能不支持异常fnsEx)
{
//设备异常不支持句柄
}
捕获(FeatureNotEnabledException fneEx)
{
//设备异常时未启用句柄
}
捕获(许可异常pEx)
{
//处理权限异常
}
捕获(例外情况除外)
{
//无法获取位置
}
}
该按钮的逻辑很简单:我从手机的GPS获取坐标,将它们传递给API,它将定居点的名称返回到其坐标,然后将名称附加到搜索中。 iOS项目没有任何问题,但Android
var location=wait Geolocation.GetLastKnownLocationAsync();
即将推出:
null


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

您是否调用了
GetLocationAsync()
首先?否则,我认为它不会返回任何内容。你能给我举个例子吗?
Geolocation.GetLocationAsync()
我指给你的Github问题—“它完全可能返回null,你应该对照它进行检查,因为提供者可能已经清除了它。这正按照预期工作。”。正如@Cheesebaron所指出的,还有其他获取位置的方法-
GetLastKnownLocationAsync()
is只是使用最后一个缓存的位置,但不保证工作。您的设备是否有GPS?是否启用了GPS?您是否有权限?您是否尝试过示例应用程序?