Reputation: 357
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: A geolocation error occured: Unauthorized
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 Code
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();
}
}
}
Upvotes: 5
Views: 6822
Reputation: 1
When you have this problem it's because you forgot to give permission. To solve that, do this in the OnCreate method :
protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
Plugin.CurrentActivity.CrossCurrentActivity.Current.Init(this, savedInstanceState);
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
Xamarin.FormsMaps.Init(this, savedInstanceState);
LoadApplication(new App());
}
Upvotes: 0
Reputation: 34013
Did you also see and do this?
This plugin leverages the Permission Plugin, which means you must add the following code to your
BaseActivity
orMainActivity
in Xamarin.Forms:Add in Activity:
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); }
From the documentation here.
Upvotes: 6
Reputation: 3064
You are probably missing the code below in your OnCreate method of the main activity.
Plugin.CurrentActivity.CrossCurrentActivity.Current.Init(this, bundle);
Upvotes: 8