【问题标题】:Google places API turn predictions into PredictionGoogle Places API 将预测转化为预测
【发布时间】:2021-10-18 05:50:58
【问题描述】:

我有这个代码来自动完成地址:

final request =
        'https://maps.googleapis.com/maps/api/place/autocomplete/json?input=$input&types=address&language=$lang&components=country:az&key=$apiKey&sessiontoken=$sessionToken';
final response = await client.get(Uri.parse(request));

if (response.statusCode == 200) {
  final result = new Map<String, dynamic>.from(json.decode(response.body));
  if (result['status'] == 'OK') {
    // compose suggestions in a list
    GoogleMapsPlaces _places = GoogleMapsPlaces(apiKey: apiKey);
    for(Prediction p in result['predictions']){
      PlacesDetailsResponse detail = await _places.getDetailsByPlaceId(p.placeId!);
      print(detail);
    }
    return result['predictions']
        .map<Suggestion>((p) => Suggestion(p['place_id'], p['description']))
        .toList();
  }

但是,这会在for 循环行中产生错误:

Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Prediction'

我想将result['predictions'] 视为预测变量,以便获得地址的坐标。这就是result['predictions'] 的样子

[{description: Yaşar Hüseynov, Baku, Azerbaijan, matched_substrings: [{length: 5, offset: 0}], place_id: EiJZYcWfYXIgSMO8c2V5bm92LCBCYWt1LCBBemVyYmFpamFuIi4qLAoUChIJWRvXZl99MEARhWIJdhJqNkgSFAoSCfkcIdZrfTBAEWts5Xpeaz80, reference: EiJZYcWfYXIgSMO8c2V5bm92LCBCYWt1LCBBemVyYmFpamFuIi4qLAoUChIJWRvXZl99MEARhWIJdhJqNkgSFAoSCfkcIdZrfTBAEWts5Xpeaz80, structured_formatting: {main_text: Yaşar Hüseynov, main_text_matched_substrings: [{length: 5, offset: 0}], secondary_text: Baku, Azerbaijan}, terms: [{offset: 0, value: Yaşar Hüseynov}, {offset: 16, value: Baku}, {offset: 22, value: Azerbaijan}], types: [route, geocode]},

【问题讨论】:

    标签: flutter api google-maps google-places-api


    【解决方案1】:

    我使用https://maps.googleapis.com/maps/api/place/details/json?place_id=$id&amp;sensor=true&amp;key=$apiKey 来获取包括坐标在内的更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-10
      • 2015-04-12
      • 1970-01-01
      • 2016-06-10
      • 2016-12-31
      • 2011-04-12
      相关资源
      最近更新 更多