如何在flutter中获得带电位置的坐标?我想要得到实时的位置坐标。怎样才能做到这一点呢?我在flutter中导入了位置包,但我只想获得实时位置的坐标。
发布于 2020-11-13 21:25:39
首先,你需要得到一个位置插件。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-13 16:18:34
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
});https://stackoverflow.com/questions/64817441
复制相似问题