0

I'm trying to develop a mobile app using Xamarin forms and part of this app is get GPS location of users.

But when I run the app I get this exception message

This functionality is not implemented in the portable version of this assembly. You should reference the NuGet package from your main application project in order to reference the platform-specific implementation.

I tried to find a solution but nothing useful. Any suggestions please ?

I added this permissions to the android app:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

Code

try
{
    var locator = CrossGeolocator.Current;
    locator.DesiredAccuracy = 100;
    var position = await locator.GetPositionAsync(TimeSpan.FromSeconds(20), null, true);
}
catch (Exception exception)
{
    Debug.WriteLine(exception.Message);
    // throw;
}
Baouche Iqbal
  • 105
  • 1
  • 13

2 Answers2

1

Possible Solution

According to the author's documentations, you need to install Current Activity Plugin for Xamarin.Android. And have following line of code in your MainActivity.cs.

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    protected override void OnCreate(Bundle bundle)
    {
        // some code
        CrossCurrentActivity.Current.Init(this, bundle);
        LoadApplication(new App());
    }

Because your demo project is running on .Net 4.5, I could not try it out. You can read the author's documenations carefully and try it again.

My Recommendation

I have looked at the Github page GeolocatorPlugin that you are using. The author states that:

The time is now with Xamarin.Essentials, which offers over 30 cross-platform native APIs in a single optimized package. I worked on this new library with an amazing team of developers and I highly highly highly recommend you check it out

So you should switch to use Xamarin.essentials plugin instead. Here is the documentation from Microsoft how to use Geolocation from that plugin.

I've also answered a similar question, you may need to reference to how can I get gps location of my device in xamarin forms

Fuong
  • 320
  • 3
  • 12
1

after several attempts i solved the problem by updating the xamarin.Forms to the latest version with the rest of nuget packages.

Baouche Iqbal
  • 105
  • 1
  • 13