user14707939
user14707939

Reputation:

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

Upvotes: 3

Views: 15062

Answers (3)

Mosawir Shah
Mosawir Shah

Reputation: 1

Geodecoder is deprecated in Dart 3, instead of Geodecoder, use Geocode as follows:

var address = await GeoCode().reverseGeocoding(latitude, longitude);

Now you can find more things by using the variable address:

 print(address.city);

print(address.countryName);

Link for Geocode

Upvotes: 0

Pranay Fating
Pranay Fating

Reputation: 84

You can use this package https://pub.dev/packages/geocoder

var address = await Geocoder.local.findAddressesFromCoordinates(coordinates);
String yourCityName = address.first.locality

Upvotes: 2

Max
Max

Reputation: 365

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){}
  ...

Upvotes: 12

Related Questions