【发布时间】:2020-03-25 03:32:15
【问题描述】:
我可以弄清楚如何将用户的纬度和经度放在一起,但需要单独获取它们以利用 Geolocator 包中的 Geolocator().distanceBetween 函数。我下面的代码不断收到错误:
未处理的异常:NoSuchMethodError:在 null 上调用了 getter 'latitude'。
接收者:空
尝试调用:纬度
Future<Position> getLocation() async {
var currentLocation;
try {
currentLocation = await geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.best);
} catch (e) {
currentLocation = null;
}
return currentLocation;
}
void calculateDistance() async {
getLocation().then((position) {
userLocation = position;
});
final double myPositionLat = userLocation.latitude;
final double myPositionLong = userLocation.longitude;
final double TPLat = 51.5148731;
final double TPLong = -0.1923663;
final distanceInMetres = await Geolocator().distanceBetween(myPositionLat, myPositionLong, TPLat, TPLong)/1000;
print(distanceInMetres);
}
【问题讨论】: