【问题标题】:Getting city and country from GoogleGeocoder response从 GoogleGeocoder 响应中获取城市和国家/地区
【发布时间】:2021-12-13 13:09:53
【问题描述】:

我需要使用 google_geocoding 包。

我的代码是:

Future<GeocodingResponse> getDatosGeocoding () async {
    var resultado= (await googleGeocoding.geocoding.getReverse(LatLon(40.714224,-73.961452))) ;
    
    return resultado;

  }

我一直在搜索包自述文件,但无法从 GeocodingResponse 中找到如何获取城市和国家/地区。

【问题讨论】:

    标签: flutter google-geocoder


    【解决方案1】:

    不幸的是,界面并没有那么精细,无法直接将城市和国家作为单独的值。

    但也许这段代码 sn-p 会帮助你:

    final response = await googleGeocoding.geocoding.getReverse(LatLon(lat, lng));
    
    if (response != null && response.results != null) {
      final geocodingResponse = response.results;
      if (geocodingResponse != null) {
        if (geocodingResponse.isNotEmpty) {
          final address = geocodingResponse[0].formattedAddress;
          if (address != null) {
            // address is available
          }
        }
      }
    }
    

    除了formattedAddress,您还可以尝试查看addressComponents 包含的值。不幸的是,documentation 不是很具体。

    【讨论】:

    • 让我试试,谢谢
    • 我得到一个空结果: if (resultado != null && resultado.results != null) { print("RESUltado1:");最终地理编码响应 = resultado.results; if (geocodingResponse != null) { print("RESUltado2:"); if (geocodingResponse.isNotEmpty) { print("RESUltado3:");最终地址 = geocodingResponse[0].formattedAddress; if (address != null) { // 地址可用 print("RESUltado4:"+address); } } } }
    • 我正在打印我们的 resultado1 和 resultado2,但不是地址
    • 如果您的 API 密钥不正确,可能会发生这种情况。
    【解决方案2】:

    我使用它,它非常适合我 地理编码:^ 2.0.0

    【讨论】:

    • 谢谢,但我需要使用问题中所说的 google_geocoding
    • 有了它,你也可以做任何你想做的事,我用它,这就是我向你推荐它的原因
    • 我知道,但由于某种原因,我需要更新的当前项目不接受该包,如果安装了地理编码包,我会在执行过程中出错
    猜你喜欢
    • 2017-09-02
    • 1970-01-01
    • 2016-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多