Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin: A geolocation error occured: Unauthorized

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();
    }
   }
  }
like image 263
abokor hassan Avatar asked Aug 23 '18 18:08

abokor hassan


1 Answers

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

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

like image 153
Siphamandla Hero Ngwenya Avatar answered Oct 23 '22 18:10

Siphamandla Hero Ngwenya