predictionToGooglePlace method

  1. @override
Future<Place> predictionToGooglePlace(
  1. Prediction p
)
override

Implementation

@override
Future<Place> predictionToGooglePlace(Prediction p) async {
  AppConfig.logger.d("");

  Place place = Place();
  String placeName = "";
  Address address = Address();
  if(p.terms.isNotEmpty) {
    placeName = p.terms.elementAt(0).value;

    if(p.terms.length == 4) {
      address = Address(
        city: p.terms.elementAt(1).value,
        state: p.terms.elementAt(2).value,
        country: p.terms.elementAt(3).value,
      );
    } else if(p.terms.length == 5) {
      address = Address(
        street: p.terms.elementAt(1).value,
        city: p.terms.elementAt(2).value,
        state: p.terms.elementAt(3).value,
        country: p.terms.elementAt(4).value,
      );
    } else if(p.terms.length == 6) {
      address = Address(
        street: p.terms.elementAt(1).value,
        neighborhood: p.terms.elementAt(2).value,
        city: p.terms.elementAt(3).value,
        state: p.terms.elementAt(4).value,
        country: p.terms.elementAt(5).value,
      );
    }

  }

  try {
    GoogleMapsPlaces places = GoogleMapsPlaces(
      apiKey: AppProperties.getGoogleApiKey(),
      apiHeaders: await const GoogleApiHeaders().getHeaders(),
    );

    PlacesDetailsResponse detail = await places.getDetailsByPlaceId(p.placeId!);

    place.name = placeName;
    place.address = address;
    place.position = Position(
        latitude: detail.result.geometry!.location.lat,
        longitude: detail.result.geometry!.location.lng,
        timestamp: DateTime.now(), accuracy: 0, altitude: 0, heading: 0, speed: 0, speedAccuracy: 0,
        altitudeAccuracy: 1, headingAccuracy: 1
    );
    AppConfig.logger.i(place.toString());
  } catch (e) {
    AppConfig.logger.e(e.toString());
  }

  return place;
}