autocomplete method

Future<Response<PlacesAutocompleteResponse>> autocomplete({
  1. required String input,
  2. String? sessiontoken,
  3. String? components,
  4. bool? strictbounds,
  5. num? offset,
  6. String? origin,
  7. String? location,
  8. num? radius,
  9. String? types,
  10. String? language = 'en',
  11. CancelToken? cancelToken,
  12. Map<String, dynamic>? headers,
  13. Map<String, dynamic>? extra,
  14. ValidateStatus? validateStatus,
  15. ProgressCallback? onSendProgress,
  16. ProgressCallback? onReceiveProgress,
})

autocomplete 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. <div class=&quot;note&quot;>Note: You can use Place Autocomplete even without a map. If you do show a map, it must be a Google map. When you display predictions from the Place Autocomplete service without a map, you must include the 'Powered by Google' logo.</div> The Place Autocomplete service can match on full words and substrings, resolving place names, addresses, and plus codes. Applications can therefore send queries as the user types, to provide on-the-fly place predictions. The returned predictions are designed to be presented to the user to aid them in selecting the desired place. You can send a Place Details request for more information about any of the places which are returned.

Parameters:

  • input - The text string on which to search. The Place Autocomplete service will return candidate matches based on this string and order results based on their perceived relevance.
  • sessiontoken - A random string which identifies an autocomplete session for billing purposes. The session begins when the user starts typing a query, and concludes when they select a place and a call to Place Details is made. Each session can have multiple queries, followed by one place selection. The API key(s) used for each request within a session must belong to the same Google Cloud Console project. Once a session has concluded, the token is no longer valid; your app must generate a fresh token for each session. If the sessiontoken parameter is omitted, or if you reuse a session token, the session is charged as if no session token was provided (each request is billed separately). We recommend the following guidelines: - Use session tokens for all autocomplete sessions. - Generate a fresh token for each session. Using a version 4 UUID is recommended. - Ensure that the API key(s) used for all Place Autocomplete and Place Details requests within a session belong to the same Cloud Console project. - Be sure to pass a unique session token for each new session. Using the same token for more than one session will result in each request being billed individually.
  • components - A grouping of places to which you would like to restrict your results. Currently, you can use components to filter by up to 5 countries. Countries must be passed as a two character, ISO 3166-1 Alpha-2 compatible country code. For example: components=country:fr would restrict your results to places within France. Multiple countries must be passed as multiple country:XX filters, with the pipe character | as a separator. For example: components=country:us|country:pr|country:vi|country:gu|country:mp would restrict your results to places within the United States and its unincorporated organized territories. <div class="note">Note: If you receive unexpected results with a country code, verify that you are using a code which includes the countries, dependent territories, and special areas of geographical interest you intend. You can find code information at <a href="https://en.wikipedia.org/wiki/List_of_ISO_3166_country_codes" target="blank" class="external">Wikipedia: List of ISO 3166 country codes or the <a href="https://www.iso.org/obp/ui/#search" target="blank" class="external">ISO Online Browsing Platform.
  • strictbounds - Returns only those places that are strictly within the region defined by location and radius. This is a restriction, rather than a bias, meaning that results outside this region will not be returned even if they match the user input.
  • offset - The position, in the input term, of the last character that the service uses to match predictions. For example, if the input is Google and the offset is 3, the service will match on Goo. The string determined by the offset is matched against the first word in the input term only. For example, if the input term is Google abc and the offset is 3, the service will attempt to match against Goo abc. If no offset is supplied, the service will use the whole term. The offset should generally be set to the position of the text caret.
  • origin - The origin point from which to calculate straight-line distance to the destination (returned as distance_meters). If this value is omitted, straight-line distance will not be returned. Must be specified as latitude,longitude.
  • location - The point around which to retrieve place information. This must be specified as latitude,longitude. <div class="note">When using the Text Search API, the location parameter may be overriden if the query contains an explicit location such as Market in Barcelona.
  • radius - Defines the distance (in meters) within which to return place results. You may bias results to a specified circle by passing a location and a radius parameter. Doing so instructs the Places service to prefer showing results within that circle; results outside of the defined area may still be displayed. The radius will automatically be clamped to a maximum value depending on the type of search and other parameters. * Autocomplete: 50,000 meters * Nearby Search: * with keyword or name: 50,000 meters * without keyword or name * rankby=prominence (default): 50,000 meters * rankby=distance: A few kilometers depending on density of area * Query Autocomplete: 50,000 meters * Text Search: 50,000 meters
  • types - You may restrict results from a Place Autocomplete request to be of a certain type by passing a types parameter. The parameter specifies a type or a type collection, as listed in the supported types below. If nothing is specified, all types are returned. In general only a single type is allowed. The exception is that you can safely mix the geocode and establishment types, but note that this will have the same effect as specifying no types. The supported types are: - geocode instructs the Place Autocomplete service to return only geocoding results, rather than business results. Generally, you use this request to disambiguate results where the location specified may be indeterminate. - address instructs the Place Autocomplete service to return only geocoding results with a precise address. Generally, you use this request when you know the user will be looking for a fully specified address. - establishment instructs the Place Autocomplete service to return only business results. - (regions) type collection instructs the Places service to return any result matching the following types: - locality - sublocality - postal_code - country - administrative_area_level_1 - administrative_area_level_2 - (cities) type collection instructs the Places service to return results that match locality or administrative_area_level_3.
  • language - The language in which to return results. * See the list of supported languages. Google often updates the supported languages, so this list may not be exhaustive. * If language is not supplied, the API attempts to use the preferred language as specified in the Accept-Language header. * The API does its best to provide a street address that is readable for both the user and locals. To achieve that goal, it returns street addresses in the local language, transliterated to a script readable by the user if necessary, observing the preferred language. All other addresses are returned in the preferred language. Address components are all returned in the same language, which is chosen from the first component. * If a name is not available in the preferred language, the API uses the closest match. * The preferred language has a small influence on the set of results that the API chooses to return, and the order in which they are returned. The geocoder interprets abbreviations differently depending on language, such as the abbreviations for street types, or synonyms that may be valid in one language but not in another. For example, utca and tér are synonyms for street in Hungarian.
  • cancelToken - A CancelToken that can be used to cancel the operation
  • headers - Can be used to add additional headers to the request
  • extras - Can be used to add flags to the request
  • validateStatus - A ValidateStatus callback that can be used to determine request success based on the HTTP status of the response
  • onSendProgress - A ProgressCallback that can be used to get the send progress
  • onReceiveProgress - A ProgressCallback that can be used to get the receive progress

Returns a Future containing a Response with a PlacesAutocompleteResponse as data Throws DioError if API call or serialization fails

Implementation

Future<Response<PlacesAutocompleteResponse>> autocomplete({
  required String input,
  String? sessiontoken,
  String? components,
  bool? strictbounds,
  num? offset,
  String? origin,
  String? location,
  num? radius,
  String? types,
  String? language = 'en',
  CancelToken? cancelToken,
  Map<String, dynamic>? headers,
  Map<String, dynamic>? extra,
  ValidateStatus? validateStatus,
  ProgressCallback? onSendProgress,
  ProgressCallback? onReceiveProgress,
}) async {
  final _path = r'/maps/api/place/autocomplete/json';
  final _options = Options(
    method: r'GET',
    headers: <String, dynamic>{
      ...?headers,
    },
    extra: <String, dynamic>{
      'secure': <Map<String, String>>[
        {
          'type': 'apiKey',
          'name': 'ApiKeyAuth',
          'keyName': 'key',
          'where': 'query',
        },
      ],
      ...?extra,
    },
    validateStatus: validateStatus,
  );

  final _queryParameters = <String, dynamic>{
    r'input':
        encodeQueryParameter(_serializers, input, const FullType(String)),
    if (sessiontoken != null)
      r'sessiontoken': encodeQueryParameter(
          _serializers, sessiontoken, const FullType(String)),
    if (components != null)
      r'components': encodeQueryParameter(
          _serializers, components, const FullType(String)),
    if (strictbounds != null)
      r'strictbounds': encodeQueryParameter(
          _serializers, strictbounds, const FullType(bool)),
    if (offset != null)
      r'offset':
          encodeQueryParameter(_serializers, offset, const FullType(num)),
    if (origin != null)
      r'origin':
          encodeQueryParameter(_serializers, origin, const FullType(String)),
    if (location != null)
      r'location': encodeQueryParameter(
          _serializers, location, const FullType(String)),
    if (radius != null)
      r'radius':
          encodeQueryParameter(_serializers, radius, const FullType(num)),
    if (types != null)
      r'types':
          encodeQueryParameter(_serializers, types, const FullType(String)),
    if (language != null)
      r'language': encodeQueryParameter(
          _serializers, language, const FullType(String)),
  };

  final _response = await _dio.request<Object>(
    _path,
    options: _options,
    queryParameters: _queryParameters,
    cancelToken: cancelToken,
    onSendProgress: onSendProgress,
    onReceiveProgress: onReceiveProgress,
  );

  PlacesAutocompleteResponse _responseData;

  try {
    const _responseType = FullType(PlacesAutocompleteResponse);
    _responseData = _serializers.deserialize(
      _response.data!,
      specifiedType: _responseType,
    ) as PlacesAutocompleteResponse;
  } catch (error, stackTrace) {
    throw DioError(
      requestOptions: _response.requestOptions,
      response: _response,
      type: DioErrorType.other,
      error: error,
    )..stackTrace = stackTrace;
  }

  return Response<PlacesAutocompleteResponse>(
    data: _responseData,
    headers: _response.headers,
    isRedirect: _response.isRedirect,
    requestOptions: _response.requestOptions,
    redirects: _response.redirects,
    statusCode: _response.statusCode,
    statusMessage: _response.statusMessage,
    extra: _response.extra,
  );
}