5

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();
    }
   }
  }
Sagar Zala
  • 4,854
  • 9
  • 34
  • 62
abokor hassan
  • 356
  • 6
  • 17

3 Answers3

8

You are probably missing the code below in your OnCreate method of the main activity.

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

Siphamandla Ngwenya
  • 2,696
  • 1
  • 16
  • 21
  • Only needed this line. Thanks Mr Ngwenya – Sazi Feb 24 '19 at 18:19
  • Ngiyabonga mngani – Adam Hey Oct 31 '19 at 19:05
  • This is kind of old maybe I should have started a new question. When I add Plugin.CurrentActivity.CrossCurrentActivity.Current.Init(this, bundle); to my main activity the bundle is I get the error bundle does not exist in current context I tried to declare a variable Bundle bundle but the error didnt go away when I try Bundle.Empty it works but Lattitude and longitude are wrong. – user2180706 Jul 02 '20 at 20:47
6

Did you also see and do this?

This plugin leverages the Permission Plugin, which means you must add the following code to your BaseActivity or MainActivity 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.

Gerald Versluis
  • 30,492
  • 6
  • 73
  • 100
0

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