如何以Xamarin表单获取设备的GPS位置? [英] How can I get GPS Location of my device in Xamarin Forms?

查看:139
本文介绍了如何以Xamarin表单获取设备的GPS位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当名为 entLocation 的输入框成为重点时,我想获取设备的经度和纬度.我正在使用 Xamarin.Essential Geolocation 来获取设备的GPS位置,但我仍然按照文档和教程进行操作,但仍然无法获取GPS位置.我在我的android清单中添加了以下权限,但仍然没有.我收到有关"Error3" 的显示警报,当无法获取位置时, Error3 是一个例外.

I want to get the Longitude and Latitude of my device when the entry box named entLocation is focused. 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. I have added permission below to my android manifest still nothing. I am getting an display alert on "Error3", Error3 is the exception when Unable to get location.

错误为" Java.Lang.NullPointerException:尝试在空对象引用上调用虚拟方法'void android.app.Activity.requestPermissions(java.lang.String [],int)' >"



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



My questions:
1. What is my error in my code?
2. Can I get my gps location offline? If yes, how can I achieve that?

uses-permission android:name ="android.permission.ACCESS_COARSE_LOCATION"
使用权限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"

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位置.

I am also working on my first Xamarin project that requires location permissions. A couple of things you should try that may help you get GPS location.

1)您需要在AndroidManifest文件中授予其他权限.这些是

1) You need to grant additional permissions in AndroidManifest file. 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) Android 6.0或更高版本以来,您需要具有运行时权限.为此,您需要在MainActivity.cs中覆盖OnStart()方法.我引用了具有6.0以上版本的Android的位置权限Xamarin.Forms.Maps .这是我所做的

2) Since Android 6.0 or above, you need to have runtime permissions. To acchieve that, you need to override OnStart() method in your MainActivity.cs. I referenced to Location permission for Android above 6.0 with Xamarin.Forms.Maps. 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);
    }

}

重要:这里的解决方案是暂时处理授予权限.首次生成和运行项目时,Visual Studio会显示异常.在设备上的允许访问位置?" 提示符下选择允许" 后,再次运行该项目时不会出现异常.我尚未处理此问题.

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.

我的极限

正如我所说,这个问题尚未完全解决.我的答案是帮助您暂时解决位置权限问题并继续前进.而且我不知道如何离线获取位置信息.

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.

这篇关于如何以Xamarin表单获取设备的GPS位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆