简体   繁体   English

FLUTTER WEB:如何在 Flutter Web 中使用 Google Places 自动完成功能?

[英]FLUTTER WEB: How To Use Google Places Autocomplete In Flutter Web?

I used almost every package for places auto complete, I am getting cross error.我几乎每个 package 都用于自动完成的地方,我遇到交叉错误。 Please let me know if there is any way to use places autocomplete In Android, iOS and web together.请让我知道是否有任何方法可以将 Android、iOS 和 web 中的位置自动完成一起使用。

 var p = await PlacesAutocomplete.show(
    radius: 500000,
    strictbounds: false,
    region: "in",
    language: "en",
    context: context,
    mode: Mode.overlay,
    apiKey: "key",
    components: [new Component(Component.country, "in")],
    types: ["(cities)"],
    hint: "Search City",
);

currently I am using this function目前我正在使用这个 function

To use the Google Places Autocomplete API in a Flutter Web application, you will need to follow these steps:要在 Flutter Web 应用程序中使用 Google Places Autocomplete API,您需要执行以下步骤:

Register for a Google Cloud Platform account and enable the Places API. Create a new project in the Google Cloud Console, and generate an API key.注册一个Google Cloud Platform账号并启用Places API。在Google Cloud Console新建一个项目,生成一个API key。 Add the google_maps_webservice package to your Flutter project's dependencies in the pubspec.yaml file.在 pubspec.yaml 文件中将 google_maps_webservice package 添加到您的 Flutter 项目的依赖项中。 Import the package and use the GoogleMapsPlaces class to make requests to the Places API. Here is an example of how to use the autocomplete feature in a Flutter Web application:导入 package 并使用 GoogleMapsPlaces class 向 Places API 发出请求。以下是如何在 Flutter Web 应用程序中使用自动完成功能的示例:

import 'package:flutter/material.dart';
import 'package:google_maps_webservice/places.dart';

class PlacesAutocompletePage extends StatefulWidget {
  @override
  _PlacesAutocompletePageState createState() => _PlacesAutocompletePageState();
}

class _PlacesAutocompletePageState extends State<PlacesAutocompletePage> {
  final _placesAutocomplete = GoogleMapsPlaces(apiKey: 'YOUR_API_KEY');
  String _placeId;

  Future<void> _getPlacePredictions() async {
    final predictions = await _placesAutocomplete.autocomplete(
      input: 'Google',
    );

    for (final prediction in predictions.predictions) {
      print(prediction.description);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Places Autocomplete'),
      ),
      body: Center(
        child: RaisedButton(
          onPressed: _getPlacePredictions,
          child: Text('Get Place Predictions'),
        ),
      ),
    );
  }
}

You need to have separate api for this google placeAutocomple您需要为这个 google placeAutocomple 单独设置 api

  ApiResponse response = await _client.get("place/autocomplete/json?input=$input"
          "&types=establishment&language=en&components=country:in&key=$kGoogleApiKey&sessiontoken=19");

and then show result in autocomplete textfield or listview.然后在自动完成文本字段或列表视图中显示结果。


  void getPlaces(input, lng,lat) {
    SearchLocation()
        .getPlaces(input: input, lang: lng,lat: lat)
        .then((value) => _placesResponse(value))
        .catchError((e) => _placesError(e));
  }

  _placesResponse(List<Place> v) {
    setState(() {
      addressList = v;
    });
  }

  _placesError(e) {
    errorSnackbar(context,e.toString());
  }

Listview Widget列表视图小部件

ListView.builder(
                    itemCount: addressList.length,
                    shrinkWrap: true,
                    itemBuilder: (ctx, index) {
                      var address = addressList[index];
                      return Padding(
                        padding: const EdgeInsets.symmetric(horizontal: 12.0),
                        child: InkWell(
                          onTap: () {
                            if(_isLoading != true) {
                              getSelectedPlace(address.placeId);
                            }
                          },
                          child: Column(
                            children: [
                              Row(
                                children: [
                                  Container(
                                      width: 25,
                                      height: 25,
                                      decoration: BoxDecoration(color: Colors.grey, borderRadius: borderRadiusAll_30),
                                      child: Center(
                                          child: Icon(
                                        CupertinoIcons.location_solid,
                                        size: 14,
                                        color: Colors.white,
                                      ))),
                                  Flexible(
                                    fit: FlexFit.tight,
                                    child: Padding(
                                      padding: const EdgeInsets.only(left: 12.0),
                                      child: Text(
                                        address.description ?? '',
                                        style: GoogleFonts.sourceSans3(textStyle: textTheme.bodyText2),
                                        textAlign: TextAlign.left,
                                      ),
                                    ),
                                  ),
                                ],
                              ),
                              Padding(
                                padding: const EdgeInsets.symmetric(vertical: 4.0),
                                child: Divider(
                                  color: MyColor.kGrey,
                                ),
                              )
                            ],
                          ),
                        ),
                      );
                    }),

For this you need to have kGoogleApiKey为此你需要有kGoogleApiKey

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM