Search code examples
flutterdartgeolocationmaps

How to get city from coordinates in flutter?


I am using GeoLocator dependency on Flutter and able to get the real-time coordinates of the location.

But I want only generalized coordinates of the city to be derived how should I do that.

The below code is providing exact realtime location when using the above mentioned dependency, which I don't require.

I want to have the city name based on the geolocation.

Please guide how should I achieve it?

 await Geolocator.getCurrentPosition().then((value) => {
                        _positionItems.add(_PositionItem(
                            _PositionItemType.position, value.toString()))
                      });

Solution

  • You can use Geocoding https://pub.dev/packages/geocoding

    import 'package:geocoding/geocoding.dart';
    import 'package:geolocator/geolocator.dart';
    
    
    ...
    position = await Geolocator.getCurrentPosition(
                desiredAccuracy: LocationAccuracy.best)
            .timeout(Duration(seconds: 5))
     
    try {
        List<Placemark> placemarks = await placemarkFromCoordinates(
            position.latitude,
            position.longitude,
          );
          print(placemarks[0]);
          ...
        }catch(err){}
      ...