predictionToGooglePlace method

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

Implementation

@override
Future<Place> predictionToGooglePlace(Prediction prediction) async {
  AppConfig.logger.d("Entering predictionToGooglePlace with prediction: ${prediction.toString()}");

  Place place = Place();

  ///DEPRECATED
  // if(p.terms?.isNotEmpty ?? false) {
  //   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: _googleApiKey,
      apiHeaders: await const GoogleApiHeaders().getHeaders(),
    );

    // placeId may be null if API returns only 'place' field (format: "places/ChIJ...")
    final effectivePlaceId = (prediction.placeId?.isNotEmpty == true)
        ? prediction.placeId!
        : (prediction.place?.replaceFirst('places/', '') ?? '');

    if (effectivePlaceId.isEmpty) {
      AppConfig.logger.w('predictionToGooglePlace: No placeId in prediction');
      return place;
    }

    PlaceDetails placeDetails = await places.getDetailsByPlaceId(
        effectivePlaceId,
        language: Sint.locale?.languageCode
    );

    place.name = placeDetails.displayName?.text ?? '' ;
    place.address = await PositionUtilities.getAddressFromFormattedAddress(placeDetails.formattedAddress ?? '');
    place.position = Position(
        latitude: placeDetails.location?.latitude ?? 0,
        longitude: placeDetails.location?.longitude ?? 0,
        timestamp: DateTime.now(), accuracy: 0, altitude: 0, heading: 0, speed: 0, speedAccuracy: 0,
        altitudeAccuracy: 1, headingAccuracy: 1
    );
    AppConfig.logger.i(place.toString());
  } catch (e, st) {
    NeomErrorLogger.recordError(e, st, module: 'neom_core', operation: 'predictionToGooglePlace');
  }

  return place;
}