如何在 ios 和 android 的 flutter 中检测模拟位置

How to detect mock location in flutter for ios and android

我正在使用位置包,google 地图在我的屏幕上颤动,我想检测用户是否使用假 gps。是否有一个包可以检测颤动中的模拟位置,可用于android 和 ios?我试过使用 TrustFall 包,但我的应用程序总是意外关闭。是否有另一种方法来检测 flutter 中的模拟位置?

你可以使用TrustLocation

权限:

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

用法:

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;

使用蒸汽:

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

示例:

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'),
         ],
       )),
     ),
   ),
 );
}
}

更多信息你可以看https://pub.dev/packages/trust_location

github link : https://github.com/wongpiwat/trust-location

我认为使用 safe_device 更好。它适用于 Android 和 IOS

使用 Geolocator 并检查 Position 对象的 isMocked 属性。