本文介绍了Xamarin:发生地理位置错误:未经授权的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取当前位置,并且正在UWP和iOS上运行,但是在Andriod上运行时,它显示了Unhandled Exception.

I'm trying to get the current location, and helpfully is working on UWP and iOS but when running it on Andriod it's showing Unhandled Exception.

Plugin.Geolocator.Abstractions.GeolocationException:发生地理位置错误:未经授权

我已经向我的android清单添加了权限,就像这样:

And I already added permissions to my android manifest and it's like this:

AndroidManifest.xml

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<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" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />

<application android:label="Location1.Android">
<meta-data android:name="com.google.android.maps.v2.API_KEY"
       android:value="**googleAPI_KEY**" />
 <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

AssemblyInfo.cs

using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Android.App;

[assembly: AssemblyTitle("Location1.Android")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Location1.Android")]
[assembly: AssemblyCopyright("Copyright ©  2014")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]

[assembly: UsesFeature("android.hardware.location", Required = false)]
[assembly: UsesFeature("android.hardware.location.gps", Required = false)]
[assembly: UsesFeature("android.hardware.location.network", Required =
 false)]


[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]


[assembly: UsesPermission(Android.Manifest.Permission.Internet)]
 [assembly:UsesPermission(Android.Manifest.Permission.WriteExternalStorage)]

XAML

enter code here<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:local="clr-namespace:Location1"
    xmlns:maps="clr-
      namespace:Xamarin.Forms.Maps;assembly=Xamarin.Forms.Maps"
         x:Class="Location1.MainPage">
<StackLayout Orientation="Vertical">
    <Button Text="Get GPS Position" Clicked="Button_Clicked" />
    <Label Text="latitude" TextColor="Gray" FontSize="30" />
    <Label x:Name="LatitudeLabel" TextColor="Gray" FontSize="50"></Label>
    <Label Text="Logitude" TextColor="Gray" FontSize="30" />
    <Label x:Name="LongitudeLabel" TextColor="Yellow" FontSize="50"></Label>
</StackLayout>

XAML代码

using Plugin.Geolocator;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xamarin.Forms;
using Xamarin.Forms.Maps;
using Plugin.Geolocator;


namespace Location1
 {
   public partial class MainPage : ContentPage
    {
    public MainPage()
    {

        InitializeComponent();
    }




    private async void Button_Clicked(object sender, EventArgs e)
    {
        await GetLocation();

    }

    private async Task GetLocation()
    {
        var locator = CrossGeolocator.Current;
        locator.DesiredAccuracy = 50;


        var position = await  locator.GetPositionAsync(TimeSpan.FromSeconds(10000));

        LongitudeLabel.Text = position.Longitude.ToString();
        LatitudeLabel.Text = position.Latitude.ToString();
    }
   }
  }

推荐答案

您是否也看到并做到了?

Did you also see and do this?

添加活动:

public override void OnRequestPermissionsResult(int requestCode,
    string[] permissions, Android.Content.PM.Permission[] grantResults)
{
    Plugin.Permissions.PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode,
        permissions, grantResults);
    base.OnRequestPermissionsResult(requestCode, permissions,
        grantResults);
}

从文档此处.

这篇关于Xamarin:发生地理位置错误:未经授权的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 13:01