Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to add initial address to PlacesAutocomplete widget #41

Open
AndresPinto2203 opened this issue Jan 14, 2024 · 1 comment
Open

How to add initial address to PlacesAutocomplete widget #41

AndresPinto2203 opened this issue Jan 14, 2024 · 1 comment

Comments

@AndresPinto2203
Copy link

in the stable version I have a controller property in which I can set an initial address, in the beta version I can't find the way to add an initial address, for example one that the user has previously defined in the edition of his profile, this property seems really useful to me

@bertrandgelinas
Copy link

i don't know if there is a better way, but what i did is get the current user location using geolocator, then add this location to currentLatLng parameters.

FutureBuilder(
        future: determinePosition(), // Future pour obtenir la position
         builder: (context, AsyncSnapshot<Position> snapshot) {
          if (snapshot.connectionState == ConnectionState.waiting) {
            return Center(
                child: CircularProgressIndicator()); 
          } else if (snapshot.hasError) {
            return Center(
                child: Text('Erreur : ${snapshot.error}')); 
          } else if (snapshot.hasData) {
            final currentPosition = snapshot.data!;
            return GoogleMapLocationPicker(
              mapType: MapType.normal,
              geoCodingApiHeaders: {
                'Content-Type': 'application/json',
                'Accept': 'application/json',
              },
              searchHintText: tr(LocaleKeys.choose_location),
              apiKey: firebase.app.options.apiKey,
              currentLatLng:
                  LatLng(currentPosition.latitude, currentPosition.longitude), // add it here
              onPlacesDetailsResponse: (pick) => onPlacePicked(pick!.result),
              onNext: (pick) => onPlacePicked(pick!),
              language: "fr",
              region: "ca",
            );
          } else {
            return Center(child: Text('Aucune donnée disponible.'));
          }
        },
      ),
Future<Position> determinePosition() async {
  bool serviceEnabled;
  LocationPermission permission;

  // Test if location services are enabled.
  serviceEnabled = await Geolocator.isLocationServiceEnabled();
  if (!serviceEnabled) {
    // Location services are not enabled don't continue
    // accessing the position and request users of the
    // App to enable the location services.
    return Future.error('Location services are disabled.');
  }

  permission = await Geolocator.checkPermission();
  if (permission == LocationPermission.denied) {
    permission = await Geolocator.requestPermission();
    if (permission == LocationPermission.denied) {
      // Permissions are denied, next time you could try
      // requesting permissions again (this is also where
      // Android's shouldShowRequestPermissionRationale
      // returned true. According to Android guidelines
      // your App should show an explanatory UI now.
      return Future.error('Location permissions are denied');
    }
  }

  if (permission == LocationPermission.deniedForever) {
    // Permissions are denied forever, handle appropriately.
    return Future.error(
        'Location permissions are permanently denied, we cannot request permissions.');
  }

  // When we reach here, permissions are granted and we can
  // continue accessing the position of the device.
  return await Geolocator.getCurrentPosition();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants