我正在尝试获取当前位置,并且正在努力开发UWP和iOS,但在Andriod上运行它时会显示Unhandled Exception。
Plugin.Geolocator.Abstractions.GeolocationException: A geolocation error occured: Unauthorized
我已经添加了我的Android清单的权限,它是这样的:
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();
}
}
}
你也看到并做到了吗?
此插件利用Permission Plugin,这意味着您必须将以下代码添加到Xamarin.Forms中的
BaseActivity
或MainActivity
:添加活动:
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); }
来自文档here。
您可能在主要活动的OnCreate方法中缺少以下代码。
Plugin.CurrentActivity.CrossCurrentActivity.Current.Init(this,bundle);