我希望大家安全。我编写了一种方法来获取用户位置,家庭位置和商品位置之间的距离,以进行购买和销售应用程序。我能够调试并获得三个值的打印。当我调用该方法计算距离时,它会返回预期的更大值。如果将动态值替换为静态值,则该方法有效。我不确定哪里出了问题:帮助...还应如何清除distanceList(将其传递给Text小部件。时间上它会显示项目在4096Km处,即使使用的值也应显示为0米或120米。尝试了所有内容,没有尝试。
Future<List> calculateDistance() async {
double itemAddressLat;
double itemAddressLong;
await FirebaseFirestore.instance
.collection(Str.ITEMS)
.get()
.then((QuerySnapshot querySnapshot) => {
querySnapshot.docs.forEach((doc) {
itemAddressLat =
double.tryParse(doc.data()[Str.ITEM_LATITUDE].toString()) ??
0.0;
itemAddressLong = double.tryParse(
doc.data()[Str.ITEM_LONGITUDE].toString()) ??
0.0;
print("ITEMS LOCATIONS....: " +
itemAddressLat.toString() +
", " +
itemAddressLong.toString());
//sample print below for itemAddressLat and itemAddressLong
// I/flutter (14429): ITEMS LOCATIONS....: -1.2675001, 22.7865
// I/flutter (14429): ITEMS LOCATIONS....: -1.2675001, 0.3299
// I/flutter (14429): ITEMS LOCATIONS....: -1.278937, 2.278937
// I/flutter (14429): ITEMS LOCATIONS....: 0.4565, 11.3422
// I/flutter (14429): ITEMS LOCATIONS....: -1.278937, 11.278937
// I/flutter (14429): ITEMS LOCATIONS....: -1.2675001, 32.3456
// I/flutter (14429): ITEMS LOCATIONS....: 0.1212, 21.2222
// I/flutter (14429): ITEMS LOCATIONS....: 0.0, 0.6765
// I/flutter (14429): ITEMS LOCATIONS....: 1.2921, 31.8219
// I/flutter (14429): ITEMS LOCATIONS....: 0.0, 0.0
// I/flutter (14429): ITEMS LOCATIONS....: null, null
//Error handling for null and empty for same fields printed above
if (itemAddressLat.toString().isNotEmpty ||
itemAddressLat.toString() != null &&
itemAddressLong.toString().isNotEmpty ||
itemAddressLong != null) {
if (usersCurrentLocationLat.toString().isNotEmpty ||
usersCurrentLocationLat.toString() != null &&
usersCurrentLocationLong.toString().isNotEmpty ||
usersCurrentLocationLong.toString() != null) {
distance = distanceBetween(itemAddressLat, itemAddressLong,
usersCurrentLocationLat, usersCurrentLocationLong) /
1000;
distance = distance.roundToDouble();
//distanceList.add(distance);
}
}
distanceList.add(distance);
})
});
print(distanceList);
return distanceList;
}
Future<void> setItemAndUserLocationLatLong() async {
await getLocationFromLocationPackage();
await getUserHomeAddress();
print("MY LOCATION COORDINATES=========: " +
usersCurrentLocationLat.toString() +
" , " +
usersCurrentLocationLong
.toString()); //I/flutter (14429): MY LOCATION COORDINATES=========: 0.3379671 , 32.6681845
print("MY HOME ADDRESS COORDINATES=========: " +
usersCurrentLocationLatFromDb.toString() +
" , " +
usersCurrentLocationLongFromDb
.toString()); //I/flutter (14429): MY HOME ADDRESS COORDINATES=========: -1.278937 , 36.7777984
print(distanceList); //prints I/flutter (14429): []
print(distanceList.length); //I/flutter (14429): 0
}
getLocationFromLocationPackage() async {
Location location = new Location();
bool _serviceEnabled;
PermissionStatus _permissionGranted;
LocationData _locationData;
_serviceEnabled = await location.serviceEnabled();
if (!_serviceEnabled) {
_serviceEnabled = await location.requestService();
if (!_serviceEnabled) {
usersCurrentLocationLat = -1.2921;
usersCurrentLocationLong = 36.8219;
return;
}
}
_permissionGranted = await location.hasPermission();
if (_permissionGranted == PermissionStatus.denied) {
_permissionGranted = await location.requestPermission();
if (_permissionGranted != PermissionStatus.granted) {
return;
}
}
_locationData = await location.getLocation();
setState(() {
usersCurrentLocationLat = _locationData.latitude;
usersCurrentLocationLong = _locationData.longitude;
});
print(usersCurrentLocationLat.toString() +
" NEW WAY " +
usersCurrentLocationLong.toString());
}
Future<void> getUserHomeAddress() async {
var firebaseUser = FirebaseAuth.instance.currentUser;
DocumentSnapshot snapshot = await FirebaseFirestore.instance
.collection(Str.USERS)
.doc(firebaseUser.uid)
.get();
var userAddressLatFromDB = snapshot.data()[Str.LAT];
var userAddressLongFromDB = snapshot.data()[Str.LNG];
setState(() {
usersCurrentLocationLatFromDb = userAddressLatFromDB;
usersCurrentLocationLongFromDb = userAddressLongFromDB;
});
}