简体   繁体   English

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

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

I want to get the Longitude and Latitude of my device when the entry box named entLocation is focused .当名为entLocation的输入框获得焦点时,我想获取设备的经度和纬度。 I am using Xamarin.Essential Geolocation to get the GPS location of my device I followed the documentation and tutorials and still I am unable to get the GPS Location.我正在使用Xamarin.Essential Geolocation来获取我设备的 GPS 位置,我按照文档和教程进行操作,但仍然无法获取 GPS 位置。 I have added permission below to my android manifest still nothing.我在下面的 android manifest 中添加了权限仍然没有。 I am getting an display alert on "Error3" , Error3 is the exception when Unable to get location .我在“Error3”上收到显示警报, Error3Unable to get location时的异常。

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 "错误是“ Java.Lang.NullPointerException: Attempt to invoke virtual method 'void android.app.Activity.requestPermissions(java.lang.String[],int)' on a null object reference



My questions:我的问题:
1. What is my error in my code? 1. 我的代码有什么错误?
2. Can I get my gps location offline? 2. 我可以离线获取我的 GPS 位置吗? If yes, how can I achieve that?如果是,我怎样才能做到这一点?

uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"使用权限 android:name="android.permission.ACCESS_COARSE_LOCATION"
uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"使用权限 android:name="android.permission.ACCESS_FINE_LOCATION"
uses-feature android:name="android.hardware.location" android:required="false"使用功能 android:name="android.hardware.location" android:required="false"
uses-feature android:name="android.hardware.location.gps" android:required="false"使用功能 android:name="android.hardware.location.gps" android:required="false"
uses-feature android:name="android.hardware.location.network" android:required="false"使用功能 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");
        }
    }

I am also working on my first Xamarin project that requires location permissions.我也在处理我的第一个需要位置权限的 Xamarin 项目。 A couple of things you should try that may help you get GPS location.您应该尝试一些可以帮助您获取 GPS 位置的方法。

My suggestions我的建议

1) You need to grant additional permissions in AndroidManifest file. 1)您需要在AndroidManifest文件中授予额外的权限。 These are这些是

<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) Since Android 6.0 or above , you need to have runtime permissions . 2)Android 6.0 或以上版本开始,您需要有运行时权限 To acchieve that, you need to override OnStart() method in your MainActivity.cs .为此,您需要覆盖MainActivity.cs OnStart()方法。 I referenced to Location permission for Android above 6.0 with Xamarin.Forms.Maps .使用 Xamarin.Forms.Maps引用了 Android 6.0 以上的位置权限 Here is what I did这是我所做的

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.第一次构建和运行项目时,Visual Studio 会显示异常。 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.我还没有处理这个问题。

My limits我的极限

As I said, the problem isn't completely solved.正如我所说,问题并没有完全解决。 My answer is to help you temporarily solve location permissions issue and move on.我的回答是帮助您暂时解决位置权限问题并继续前进。 And I don't know how to get location offline.而且我不知道如何离线获取位置信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM