0

如何在颤振中获取实时位置的坐标?我想获取实时位置坐标。可以做些什么来得到这个?我在颤振中导入了位置包,但我只想获取实时位置的坐标。

4

2 回答 2

1

首先你需要一个定位插件。 https://pub.dev/packages/location。我还建议使用 Provider 插件并创建模型文件。会更容易管理。此外,您必须检查设备的权限状态。您将通过链接获得您想了解的所有信息

class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

 Location _location = Location();
 LocationData currentLocation;


@override
  void initState() {
    location = new Location();
    location.onLocationChanged.listen((LocationData cLoc) {
      currentLocation = cLoc;

      });

    super.initState();
  }


  @override


Widget build(BuildContext context) {
        return Center(
  child: Text(
      'Location: Lat${currentLocation.latitude}, Long: ${currentLocation.longitude}'),);}}
          

    
于 2020-11-13T13:25:39.100 回答
0
Location location = new Location();

bool _serviceEnabled;
PermissionStatus _permissionGranted;
LocationData _locationData;

_serviceEnabled = await location.serviceEnabled();
if (!_serviceEnabled) {
  _serviceEnabled = await location.requestService();
  if (!_serviceEnabled) {
    return;
  }
}

_permissionGranted = await location.hasPermission();
if (_permissionGranted == PermissionStatus.denied) {
  _permissionGranted = await location.requestPermission();
  if (_permissionGranted != PermissionStatus.granted) {
    return;
  }
}

location.onLocationChanged.listen((LocationData currentLocation) {
  // Use current location
});

于 2020-11-13T08:18:34.483 回答