如何在 Xamarin Forms 中获取设备的 GPS 位置?

How can I get GPS Location of my device in Xamarin Forms?

当名为 entLocation 的输入框 focused 时,我想获取设备的经度和纬度。我正在使用 Xamarin.Essential Geolocation 获取设备的 GPS 位置 我按照文档和教程进行操作,但仍然无法获取 GPS 位置。我已将下面的权限添加到我的 android 清单中,但仍然没有。我在 "Error3" 上收到显示警报,Error3Unable to get location[=27] 时的异常=].

The error is "Java.Lang.NullPointerException: Attempt to invoke virtual method 'void android.app.Activity.requestPermissions(java.lang.String[],int)' on a null object reference"



我的问题:
1. 我的代码有什么错误?
2. 我可以离线获取我的 gps 位置吗?如果是,我该如何实现?

uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"
uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"
uses-feature android:name="android.hardware.location" android:required="false"
uses-feature android:name="android.hardware.location.gps" android:required="false"
uses-feature android:name="android.hardware.location.network" android:required="false"

private async void entLocation_FocusedAsync(object sender, FocusEventArgs e)
    {
        try
        {
            var request = new GeolocationRequest(GeolocationAccuracy.Medium);
            var location = await Geolocation.GetLocationAsync(request);

            if (location != null)
            {
                entLocation.Text = location.Longitude.ToString() + "," + location.Latitude.ToString();
            }
        }
        catch (FeatureNotSupportedException fnsEx)
        {
            await DisplayAlert("Error1", fnsEx.ToString(), "ok");
        }
        catch (PermissionException pEx)
        {
            await DisplayAlert("Error2", pEx.ToString(), "ok");
        }
        catch (Exception ex)
        {
            await DisplayAlert("Error3", ex.ToString(), "ok");
        }
    }

我还在处理我的第一个需要位置权限的 Xamarin 项目。您应该尝试一些可能有助于您获取 GPS 位置的方法。

我的建议

1) 您需要在 AndroidManifest 文件中授予额外的权限。这些是

<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />

2) 由于 Android 6.0 或更高版本,您需要具有 运行时权限。为此,您需要覆盖 MainActivity.cs 中的 OnStart() 方法。我参考了 。这是我所做的

protected override void OnStart()
{
    base.OnStart();
    if(CheckSelfPermission(Manifest.Permission.AccessCoarseLocation) != (int)Permission.Granted)
    {
        RequestPermissions(new string[] { Manifest.Permission.AccessCoarseLocation, Manifest.Permission.AccessFineLocation }, 0);
    }

}

Important: My solution here is just temporarily handle granting permissions. Visual Studio would show an exception when you build and run the project first time. After you choose "allow" from "Allow access location?" prompt on the device, you would not get the exception when run the project again. I have not handled this issue yet.

我的极限

正如我所说,问题并没有完全解决。我的答案是帮助您暂时解决位置权限问题并继续前进。而且我不知道如何离线定位。