getPlaceDetailFromId method

Future<Place> getPlaceDetailFromId(
  1. String placeId
)

Requests full address info from Google Places API for the specified placeId and returns a Place object returned info.

Implementation

Future<Place> getPlaceDetailFromId(String placeId) async {
  // if you want to get the details of the selected place by place_id
  final Map<String, dynamic> parameters = <String, dynamic>{
    'place_id': placeId,
    'fields': 'name,formatted_address,address_component,geometry',
    'key': mapsApiKey,
    'sessiontoken': sessionToken
  };
  final Uri request = Uri(
      scheme: 'https',
      host: 'maps.googleapis.com',
      path: '/maps/api/place/details/json',

      //PlaceApiNew     host: 'places.googleapis.com',
      //PlaceApiNew     path: '/v1/places/$placeId',

      queryParameters: parameters);

  if (debugJson) {
    debugPrint(request.toString());
  }

  final response = await client.get(request);
  /* PlaceApiNew:
      , headers: {
          'X-Goog-Api-Key': mapsApiKey,
          'X-Goog-FieldMask': 'displayName,formattedAddress',
    });
  PlaceApiNew */

  if (response.statusCode == 200) {
    final result = json.decode(response.body);
    if (result['status'] == 'OK') {
      if (debugJson) {
        printJson(result['result'],
            title: 'GOOGLE MAP API RETURN VALUE result["result"]');
      }
      final components =
          result['result']['address_components'] as List<dynamic>;

      // build result
      final place = Place();

      place.formattedAddress = result['result']['formatted_address'];
      place.name = result['result']['name'];
      place.lat = result['result']['geometry']['location']['lat'] as double;
      place.lng = result['result']['geometry']['location']['lng'] as double;

      for (var component in components) {
        final List type = component['types'];
        if (type.contains('street_address')) {
          place.streetAddress = component['long_name'];
        }
        if (type.contains('street_number')) {
          place.streetNumber = component['long_name'];
        }
        if (type.contains('route')) {
          place.street = component['long_name'];
          place.streetShort = component['short_name'];
        }
        if (type.contains('sublocality') ||
            type.contains('sublocality_level_1')) {
          place.vicinity = component['long_name'];
        }
        if (type.contains('locality')) {
          place.city = component['long_name'];
        }
        if (type.contains('administrative_area_level_2')) {
          place.county = component['long_name'];
        }
        if (type.contains('administrative_area_level_1')) {
          place.state = component['long_name'];
          place.stateShort = component['short_name'];
        }
        if (type.contains('country')) {
          place.country = component['long_name'];
        }
        if (type.contains('postal_code')) {
          place.zipCode = component['long_name'];
        }
        if (type.contains('postal_code_suffix')) {
          place.zipCodeSuffix = component['long_name'];
        }
      }

      place.zipCodePlus4 ??=
          '${place.zipCode}${place.zipCodeSuffix != null ? '-${place.zipCodeSuffix}' : ''}';
      if (place.streetNumber != null) {
        place.streetAddress ??= '${place.streetNumber} ${place.streetShort}';
        place.formattedAddress ??=
            '${place.streetNumber} ${place.streetShort}, ${place.city}, ${place.stateShort} ${place.zipCode}';
        place.formattedAddressZipPlus4 ??=
            '${place.streetNumber} ${place.streetShort}, ${place.city}, ${place.stateShort} ${place.zipCodePlus4}';
      }
      return place;
    }
    throw Exception(result['error_message']);
  } else {
    throw Exception('Failed to fetch suggestion');
  }
}