showLocationPicker function

Future<LocationResult?> showLocationPicker(
  1. BuildContext context,
  2. String apiKey, {
  3. LatLng initialCenter = const LatLng(45.521563, -122.677433),
  4. double initialZoom = 16,
  5. bool requiredGPS = false,
  6. List<String> countries = const ["IN"],
  7. bool myLocationButtonEnabled = false,
  8. bool layersButtonEnabled = false,
  9. bool automaticallyAnimateToCurrentLocation = true,
  10. String? mapStylePath,
  11. Color appBarColor = Colors.transparent,
  12. BoxDecoration? searchBarBoxDecoration,
  13. String? hintText,
  14. Widget resultCardConfirmIcon = const Icon(Icons.arrow_forward),
  15. Alignment resultCardAlignment = Alignment.bottomCenter,
  16. EdgeInsets resultCardPadding = const EdgeInsets.all(16.0),
  17. Decoration? resultCardDecoration,
  18. String language = 'en',
  19. LocationAccuracy desiredAccuracy = LocationAccuracy.best,
})

Returns a LatLng object of the location that was picked.

The apiKey argument API key generated from Google Cloud Console. You can get an API key here

initialCenter The geographical location that the camera is pointing until the current user location is know if you want to change this set automaticallyAnimateToCurrentLocation to false.

Implementation

Future<LocationResult?> showLocationPicker(
  BuildContext context,
  String apiKey, {
  LatLng initialCenter = const LatLng(45.521563, -122.677433),
  double initialZoom = 16,
  bool requiredGPS = false,
  List<String> countries = const ["IN"],
  bool myLocationButtonEnabled = false,
  bool layersButtonEnabled = false,
  bool automaticallyAnimateToCurrentLocation = true,
  String? mapStylePath,
  Color appBarColor = Colors.transparent,
  BoxDecoration? searchBarBoxDecoration,
  String? hintText,
  Widget resultCardConfirmIcon = const Icon(Icons.arrow_forward),
  Alignment resultCardAlignment = Alignment.bottomCenter,
  EdgeInsets resultCardPadding = const EdgeInsets.all(16.0),
  Decoration? resultCardDecoration,
  String language = 'en',
  LocationAccuracy desiredAccuracy = LocationAccuracy.best,
}) async {
  final results = await Navigator.of(context).push(
    MaterialPageRoute<dynamic>(
      builder: (BuildContext context) {
        // print('[LocationPicker] [countries] ${countries.join(', ')}');
        return LocationPicker(
          apiKey,
          initialCenter: initialCenter,
          initialZoom: initialZoom,
          requiredGPS: requiredGPS,
          myLocationButtonEnabled: myLocationButtonEnabled,
          layersButtonEnabled: layersButtonEnabled,
          automaticallyAnimateToCurrentLocation:
              automaticallyAnimateToCurrentLocation,
          mapStylePath: mapStylePath,
          appBarColor: appBarColor,
          hintText: hintText,
          searchBarBoxDecoration: searchBarBoxDecoration,
          resultCardConfirmIcon: resultCardConfirmIcon,
          resultCardAlignment: resultCardAlignment,
          resultCardPadding: resultCardPadding,
          resultCardDecoration: resultCardDecoration,
          countries: countries,
          language: language,
          desiredAccuracy: desiredAccuracy,
        );
      },
    ),
  );

  if (results != null && results.containsKey('location')) {
    return results['location'];
  } else {
    return null;
  }
}