placeAutocomplete method

Future<PlacesAutocompleteResponse> placeAutocomplete({
  1. required String input,
  2. String? language,
  3. List<String>? components,
  4. LatLngLiteral? location,
  5. double? radius,
  6. int? offset,
  7. String? region,
  8. String? sessionToken,
  9. bool? strictBounds,
  10. List<String>? types,
})

The Place Autocomplete service is a web service that returns place predictions in response to an HTTP request. The request specifies a textual search string and optional geographic bounds. The service can be used to provide autocomplete functionality for text-based geographic searches, by returning places such as businesses, addresses and points of interest as a user types.

Implementation

Future<PlacesAutocompleteResponse> placeAutocomplete({
  required String input,
  String? language,
  List<String>? components,
  LatLngLiteral? location,
  double? radius,
  int? offset,
  String? region,
  String? sessionToken,
  bool? strictBounds,
  List<String>? types,
}) async {
  final qp = <String, dynamic>{
    'input': input,
    if (language != null) 'language': language,
    if (components != null) 'components': components,
    if (location != null) 'location': '${location.lat},${location.lng}',
    if (radius != null) 'radius': radius,
    if (offset != null) 'offset': offset,
    if (region != null) 'region': region,
    'sessiontoken': sessionToken ?? generateSessionToken(),
    if (strictBounds != null) 'strictBounds': strictBounds,
    if (types != null) 'types': types,
  };

  try {
    final data = await doGet(
      path: '/maps/api/place/autocomplete/json',
      params: qp,
    );

    return PlacesAutocompleteResponse.fromMap(data);
  } catch (error) {
    final gr = PlacesAutocompleteResponse.fromError(error.toString());
    return gr;
  }
}