Skip to content

officialdoniald/Xamarin.Forms.MockLocation

Repository files navigation

Xamarin.MockLocationPlugin

Mocking Location with Xamarin Forms

This will only work on Android. You can use this source code just in Android too, but this NuGet Package is especially for Xamarin.Forms.

  1. Set your AndroidManifest.xml:

https://github.com/officialdoniald/Xamarin.Forms.MockLocation/blob/master/Xamarin.Forms.MockLocation.Mobile/Xamarin.Forms.MockLocation.Mobile.Android/Properties/AndroidManifest.xml

Deploy your app to the phone and close it.

One more important thing: only the System app can send mock locations, so we have to call for the operation system, that this will send mock locations. So if you haven’t already, let’s enable developer mode on your phones and go to developer options.

Select the Mock Location, and select you app. Run the app again and it will work.

  1. Request permisson: you can use the Xamarin.Essentials plugin for this, but you can use this in Xamarin.Android project, in the MainActivity last row:

ActivityCompat.RequestPermissions(this, new String[] { Manifest.Permission.AccessFineLocation }, 1);

  1. Detect mock location:

Thread thread = new Thread(async () => { while (true) { var request = new GeolocationRequest(GeolocationAccuracy.Medium); var location = await Geolocation.GetLocationAsync(request);

if (location != null)
{
  if (location.IsFromMockProvider)
  {
    //Detect mocking location
  }
  else
  {
     //Detect normal location
  }
}

} });

  1. Sending mock lockation from Standard Library:

DependencyService.Get().SendMockLocation(new IMockLocationPlugin.MockPosition() { Longitude = 24.234234, Latitude = 46.3213123, Accuracy = 1.0f, Altitude = 15, Bearing = 5f, Speed = 50f });

About

Mocking Location with Xamarin Forms

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages