Flutter 如何使用地理定位器包获取纬度、经度?
Flutter how to get latitude, longitude using geolocator package?
Flutter how to get latitude, longitude using geolocator package?
Already gave permissions both android and ios, downloaded package using pubspec.yaml. I don't understand why can't print longitude and latitude to console? tried write print(position), print(position.longitude()), print(position.latitue()).
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
class LoadingScreen extends StatefulWidget {
@override
_LoadingScreenState createState() => _LoadingScreenState();
}
class _LoadingScreenState extends State<LoadingScreen> {
void getLocation() async {
print('Printing text before getCurrentLocation()');
Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.low);
print(position);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: RaisedButton(
color: Colors.red,
onPressed: () {
getLocation();
},
child: Text('Get Location'),
),
),
);
}
} ```
应该是position.latitude
或者position.longitude
。您使用了 position.longitude()
& position.latitude()
,这是不正确的。
此外,您需要为 onPressed
回调添加 async-await
。我假设您已经在清单中添加了权限并授予了必要的权限。
方法
void getLocation() async {
Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.low);
print(position.latitude);
print(position.longitude);
}
小部件
RaisedButton(
color: Colors.red,
onPressed: () async {
await getLocation();
},
child: Text('Get Location'),
),
Flutter how to get latitude, longitude using geolocator package? Already gave permissions both android and ios, downloaded package using pubspec.yaml. I don't understand why can't print longitude and latitude to console? tried write print(position), print(position.longitude()), print(position.latitue()).
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
class LoadingScreen extends StatefulWidget {
@override
_LoadingScreenState createState() => _LoadingScreenState();
}
class _LoadingScreenState extends State<LoadingScreen> {
void getLocation() async {
print('Printing text before getCurrentLocation()');
Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.low);
print(position);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: RaisedButton(
color: Colors.red,
onPressed: () {
getLocation();
},
child: Text('Get Location'),
),
),
);
}
} ```
应该是position.latitude
或者position.longitude
。您使用了 position.longitude()
& position.latitude()
,这是不正确的。
此外,您需要为 onPressed
回调添加 async-await
。我假设您已经在清单中添加了权限并授予了必要的权限。
方法
void getLocation() async {
Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.low);
print(position.latitude);
print(position.longitude);
}
小部件
RaisedButton(
color: Colors.red,
onPressed: () async {
await getLocation();
},
child: Text('Get Location'),
),