gpt4 book ai didi

Xamarin:发生地理定位错误:未经授权

转载 作者:行者123 更新时间:2023-12-01 08:46:37 24 4
gpt4 key购买 nike

我正在尝试获取当前位置,并且在 UWP 和 iOS 上工作很有帮助,但是在 Andriod 上运行它时,它显示未处理的异常。

Plugin.Geolocator.Abstractions.GeolocationException: A geolocation error occured: Unauthorized

我已经为我的 android manifest 添加了权限,它是这样的:

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();
}
}
}

最佳答案

您的 中可能缺少以下代码OnCreate 主要事件的方法。

Plugin.CurrentActivity.CrossCurrentActivity.Current.Init(this, bundle);

关于Xamarin:发生地理定位错误:未经授权,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51992016/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com