这是我使用地理定位器获取当前位置的函数
import 'package:geolocator/geolocator.dart';
Future<Position> determinePosition() async {
LocationPermission permission;
permission = await Geolocator.checkPermission();
if (permission == LocationPermission.denied) {
permission = await Geolocator.requestPermission();
if (permission == LocationPermission.denied) {
return Future.error('Location permissions are denied');
}
}
if (permission == LocationPermission.deniedForever) {
return Future.error(
'Location permissions are permanently denied, we cannot request permissions.');
}
Position position = await Geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.high);
return position;
}
该函数返回其纬度和经度,但是,我无法将其分配给 Firestore 中的位置字段
final crab = <String, Object>{
"species": label,
"edibility": edibility,
"location": GeoPoint() // i want to put the result here,
"timestamp": Timestamp.now()
};
FirebaseFirestore.instance.collection('crabData').add(crab);
您的
determinePosition
返回一个 Position
对象,而 Firestore 需要一个 GeoPoint
对象。因此,您必须将信息从 Position
转换为 GeoPoint
:
let pos = await determinePosition();
let loc = GeoPoint(pos.latitude, pos.longitude);
此后,您可以将
loc
传递到 Firestore。