flutter插件location


这个Flutter插件可以处理Android和iOS上的位置。它还在位置更改时提供回调。

Android

要在Android中使用此插件,您必须在AndroidManifest.xml中添加此权限:

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

实例

var currentLocation = <String, double>{};

var location = new Location();

// Platform messages may fail, so we use a try/catch PlatformException.
try {
  currentLocation = await location.getLocation;
} on PlatformException {
  currentLocation = null;
}

当您的职位发生变化时,您还可以获得连续的回调:

var location = new Location();

location.onLocationChanged().listen((Map<String,double> currentLocation) {
  print(currentLocation["latitude"]);
  print(currentLocation["longitude"]);
  print(currentLocation["accuracy"]);
  print(currentLocation["altitude"]);
  print(currentLocation["speed"]);
  print(currentLocation["speed_accuracy"]); // Will always be 0 on iOS
});