我是Ffltter的新手,我正在try 建立一个简单的谷歌 map 应用程序.我已经在这款应用上实现了谷歌 map ,它运行得非常完美.

但是现在我想添加Google map 自动完成功能,但是我找不到一个简单的教程或示例来关注它.

我有一个TextField,我想根据用户键入的内容在它下面显示地点和地址.

在显示结果后,我需要得到它的纬度和经度,以便在 map 上标记.下面的代码表示我的BottomSheet,它包含我的TexField,需要在一些书面文本之后实现下面的一些列表.

void _settingModalBottomSheet(context) {
    double statusBarHeight = MediaQuery.of(context).padding.top;

    showModalBottomSheet(
      context: context,
      builder: (builder) {
        return Container(
          padding: EdgeInsets.only(top: statusBarHeight),
          color: Colors.transparent,
          child: Container(
            height: MediaQuery.of(context).size.height,
            decoration: BoxDecoration(
              color: Colors.blueAccent,
              borderRadius: BorderRadius.only(
                topLeft: const Radius.circular(10.0), topRight: const Radius.circular(10.0))),
              child: Column(
                children: <Widget>[
                  Padding(
                    padding: const EdgeInsets.only(top: 8.0, left: 8.0, right: 8.0),
                    child: Container(
                      height: 50.0,
                      width: double.infinity,
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(10.0),
                        color: Colors.white
                      ),
                      child: TextField(
                        textInputAction: TextInputAction.search,
                        decoration: InputDecoration(
                          hintText: "Para onde vamos?",
                          border: InputBorder.none,
                          contentPadding: EdgeInsets.only(left: 15.0, top: 15.0),
                          suffixIcon: IconButton(
                            icon: Icon(Icons.search),
                            onPressed: searchAndNavigate,
                            iconSize: 30.0,
                          )
                        ),
                        onChanged: (val) {
                          setState(() {
                            searchAddr = val;
                          }
                        );
                      },
                      onSubmitted: (term) {
                        searchAndNavigate();
                      },
                    ),
                  ),
                ),
              ],
            )
          ),
        );
      }
    );
  }

Current screen

推荐答案

您可以使用flutter_google_places插件,它会在您键入时显示自动完成列表中的位置,并返回所选位置/地址的LATE和LONG.

=======工作代码=======

  1. 添加flutter_google_places个插件并将其导入dart文件.
  2. 添加geo_coder个插件,并将其导入相同的DART文件中.(访问地理编码服务需要)
  3. 为您的项目生成Google API密钥.

Main.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);
    }
  }
}

结果:

当你点击"查找地址"按钮时,它会打开一个带有内置搜索应用程序栏的新屏幕,你可以在其中键入你要查找的地址/地点,它会在自动完成列表中显示相应的结果,并打印出你所选地点的latlong.

enter image description here

lat: 52.3679843
lng: 4.9035614

Flutter相关问答推荐

使用Bg晃动背景图像边界

壁炉有序在Flutter

相同的Flutter 代码库,但Firebase登录适用于Web应用程序,但无法登录iOS应用程序

从外部类动态更改IconButton的图标

flutter -当使用SingleChildScrollView包装我的列小部件时,它不会填充整个高度

查询满足AND条件的文档的FiRestore

在Fighter中,我如何在窗口的顶部和底部排列Widget?

分页控制器在到达页面末尾之前或在初始化页面时对每个索引调用API

在Flutter 中更改扩展瓷砖的高度

如何使DropDownMenu宽度等于TextFormfield?

在使用FutureProvider时,我想显示用于加载的指示器,但无法自定义大小

按一下按钮即可更新 fl_chart

为什么我的Flutter 动画过渡没有发生?

Flutter:注册 UI 出现 UI 问题(将注册框提升到顶部并向电话号码 pin 码添加一个框)

Flutter中pubspec.yaml文件中的name参数是什么?

如何将此文本按钮剪辑在堆栈中的圆形头像上方?

如何在 flutter 的 onChanged(更改字符)处更改 TextField 边框 colored颜色 ?

Android: 从 URI 获取音频元数据

如何在 cupertino switch flutter 中添加文本

如何在 ElevatedButton 上设置背景 colored颜色 动画?