mohesu / map_location_picker

Map location picker for flutter Based on google_maps_flutter
Apache License 2.0
46 stars 68 forks source link

How to add initial address to PlacesAutocomplete widget #41

Open AndresPinto2203 opened 7 months ago

AndresPinto2203 commented 7 months ago

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 commented 2 days ago

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();
}