【问题标题】:Flutter get city, country from autocompleteFlutter从自动完成获取城市,国家
【发布时间】:2021-01-03 05:08:14
【问题描述】:

我有一个位置自动填充器,但我找不到城市名称、国家/地区和所有详细信息的存储位置。我想将它们转移到诸如 city = name.city 之类的变量中,但我找不到从哪里获取它。

程序做什么,它得到地址的长和纬度。我试着弄乱他们,但我仍然找不到他们存储详细信息的位置。

这是我的代码

 import 'package:google_maps_webservice/places.dart';
import 'package:geocoder/geocoder.dart';
import 'package:flutter_google_places/flutter_google_places.dart';

   void main() => runApp(MyApp());

const kGoogleApiKey = "Api_key";

GoogleMapsPlaces _places = GoogleMapsPlaces(apiKey: kGoogleApiKey);

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: demo(),
      ),
    );
  }
}

class demo extends StatefulWidget {
  @override
  demoState createState() => new demoState();
}

class demoState extends State<demo> {
  @override
  Widget build(BuildContext context) {

    return Scaffold(
      body: Container(
        alignment: Alignment.center,
        child: RaisedButton(
          onPressed: () async {
            // show input autocomplete with selected mode
            // then get the Prediction selected
            Prediction p = await PlacesAutocomplete.show(
                context: context, apiKey: kGoogleApiKey);
            displayPrediction(p);
          },
          child: Text('Find address'),

        )
      )
    );
  }

  Future<Null> displayPrediction(Prediction p) async {
    if (p != null) {
      PlacesDetailsResponse detail =
      await _places.getDetailsByPlaceId(p.placeId);

      var placeId = p.placeId;
      double lat = detail.result.geometry.location.lat;
      double lng = detail.result.geometry.location.lng;

      var address = await Geocoder.local.findAddressesFromQuery(p.description);

      print(lat);
      print(lng);
    }
  }
}

【问题讨论】:

    标签: flutter


    【解决方案1】:

    GoogleMapsApi Place Details 不保证响应中的城市名称,但您可以访问国家名称和国家 ISO 代码,如下所示

    Future<Null> displayPrediction(Prediction p) async {
      if (p != null) {
        PlacesDetailsResponse detail = await _places.getDetailsByPlaceId(p.placeId);
    
        var placeId = p.placeId;
        String countryIso, countryName;
        double lat = detail.result.geometry.location.lat;
        double lng = detail.result.geometry.location.lng;
    
        var address = await Geocoder.local.findAddressesFromQuery(p.description);
    
        for (var adrComp in detail.result.addressComponents) {
          if (adrComp.types.contains("country")) {
            countryIso = adrComp.shortName;
            countryName = adrComp.longName;
          }
        }
    
        print(lat);
        print(lng);
      }
    }
    

    【讨论】:

    • 我需要在 pladeDetails 中添加什么才能工作,因为现在它不起作用
    • 现在我得到了这个:W/IInputConnectionWrapper(1893):getTextBeforeCursor on inactive InputConnection W/IInputConnectionWrapper(1893):getSelectedText on inactive InputConnection W/IInputConnectionWrapper(1893):getTextAfterCursor on inactive InputConnection W/IInputConnectionWrapper (1893):beginBatchEdit 在非活动 InputConnection W/IInputConnectionWrapper(1893):getTextBeforeCursor 在非活动 InputConnection W/IInputConnectionWrapper(1893):endBatchEdit 在非活动 InputConnection
    • 问题是case sensitive。尝试用“国家”替换“国家”
    • 你知道我为什么会得到这个异常吗?
    猜你喜欢
    • 2016-07-16
    • 1970-01-01
    • 2017-03-20
    • 2020-08-14
    • 1970-01-01
    • 2013-01-12
    • 2013-02-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多