gpt4 book ai didi

google-maps - 如何从纬度和经度中提取位置名称

转载 作者:IT王子 更新时间:2023-10-29 07:10:10 27 4
gpt4 key购买 nike

我在 flutter 中使用地理定位插件提取了经度和纬度。但现在我需要根据这些经度和纬度创建地名。

我尝试使用地理编码器插件,但是

  final coordinates = new Coordinates(latitude, longitude);
var addresses = Geocoder.local.findAddressesFromCoordinates(coordinates);

var first = addresses.first;

上面一行给出了错误,说 getter first is not defined for class Future<>

      print("${first.featureName} : ${first.addressLine}");

如何使用这些经纬度并在 flutter 中转换为地址?

最佳答案

findAddressesFromCoordinates 返回 Future在这种情况下。

你可以让你的函数或方法async:

void yourFunction() async {
final coordinates = new Coordinates(latitude, longitude);
var addresses = await Geocoder.local.findAddressesFromCoordinates(coordinates);

var first = addresses.first;
print("${first.featureName} : ${first.addressLine}");
}

在这种情况下,您需要在方法调用前使用 await 关键字,这将使函数仅在您的对象之后继续运行地址已被检索。

另一种选择是在 Future 上使用 then 方法,它看起来像这样:

void yourFunction() {
final coordinates = new Coordinates(latitude, longitude);
Geocoder.local.findAddressesFromCoordinates(coordinates).then((addresses) {
var first = addresses.first;
print("${first.featureName} : ${first.addressLine}");
});
}

在这种情况下,您将回调传递给 then 方法,一旦返回,该方法将使用 findAddressesFromCoordinates 的结果执行,但是 yourFunction 本身将继续运行。

关于google-maps - 如何从纬度和经度中提取位置名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50898722/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com