I am using package of location and google maps flutter in my screen and I want to detect wether user using fake gps or not.. Is there a package that can detect mock location in flutter that available in android and ios? I have tried using TrustFall package but my app always close unexpectedly.. is there another way to detect mock location in flutter?
Asked
Active
Viewed 1.1k times
3 Answers
6
Use Geolocator and check the Position object's isMocked property.

Sachin
- 1,307
- 13
- 23
-
Please add example – John Bryan Jul 25 '22 at 11:58
-
@JohnBryan: example available in Geolocator dev page. – Sachin Aug 02 '22 at 08:09
-
1Oh I found it appreciate it, Sir – John Bryan Aug 03 '22 at 07:06
-
It's doc says it will always be false on iOS. What does it mean ? Can location not be mocked in iOS (without Jaibreak) or this property cannot detect it ? Can someone please confirm. – robben May 23 '23 at 05:58
-
```_determinePosition().then((position) { if (position.isMocked) { //.. Show error message return; }``` In case you can find the documentation, here is the code – Tri yulianto Aug 09 '23 at 14:00
5
I think better to use safe_device. It's working on both Android and IOS

BIS Tech
- 17,000
- 12
- 99
- 148
-
it uses trust_location please check example here https://github.com/ufukhawk/safe_device/blob/main/pubspec.yaml, and trust_location only work for Android. – Abed Putra Aug 17 '22 at 03:58
4
you can use TrustLocation
permissions:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
usage :
import 'package:trust_location/trust_location.dart';
/* Assuming in an async function */
/// query the current location.
LatLongPosition position = await TrustLocation.getLatLong;
/// check mock location on Android device.
bool isMockLocation = await TrustLocation.isMockLocation;
using steam:
// input seconds into parameter for getting location with repeating by timer.
// this example set to 5 seconds.
TrustLocation.start(5);
/// the stream getter where others can listen to.
TrustLocation.onChange.listen((values) =>
print('${values.latitude} ${values.longitude} ${values.isMockLocation}')
);
/// stop repeating by timer
TrustLocation.stop();
Example:
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:trust_location/trust_location.dart';
import 'package:location_permissions/location_permissions.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _latitude;
String _longitude;
bool _isMockLocation;
/// initialize state.
@override
void initState() {
super.initState();
requestLocationPermission();
// input seconds into parameter for getting location with repeating by timer.
// this example set to 5 seconds.
TrustLocation.start(5);
getLocation();
}
/// get location method, use a try/catch PlatformException.
Future<void> getLocation() async {
try {
TrustLocation.onChange.listen((values) => setState(() {
_latitude = values.latitude;
_longitude = values.longitude;
_isMockLocation = values.isMockLocation;
}));
} on PlatformException catch (e) {
print('PlatformException $e');
}
}
/// request location permission at runtime.
void requestLocationPermission() async {
PermissionStatus permission =
await LocationPermissions().requestPermissions();
print('permissions: $permission');
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Trust Location Plugin'),
),
body: Padding(
padding: const EdgeInsets.all(20.0),
child: Center(
child: Column(
children: <Widget>[
Text('Mock Location: $_isMockLocation'),
Text('Latitude: $_latitude, Longitude: $_longitude'),
],
)),
),
),
);
}
}
for more information you can see https://pub.dev/packages/trust_location
github link : https://github.com/wongpiwat/trust-location

Farzad Kamali
- 751
- 2
- 6
- 24
-
2how about trust location for ios? I also need detect mock location in ios platform – wahyu May 14 '20 at 06:45
-
1This package requires minSdkVersion: 21 while we are using minSdkVersion: 18 – Faizan Mubasher Feb 24 '21 at 08:57
-
1
-