Neeraj Yadav
Neeraj Yadav

Reputation: 422

How to extract location name from lat and lon

I have extracted longitude and latitude using geolocation plugin in flutter. But now I need to create name of place from those longitude and latitude.

I tried using geocoder plugin, but

  final coordinates = new Coordinates(latitude, longitude);
          var addresses =  Geocoder.local.findAddressesFromCoordinates(coordinates);

      var first = addresses.first; 

above line is giving error saying getter first is not defined for class Future<>

      print("${first.featureName} : ${first.addressLine}");

How can I use those latitude and longitude and convert into address in flutter?

Upvotes: 2

Views: 7386

Answers (4)

A_Rush
A_Rush

Reputation: 378

With the help of geocoding you can get Placemark using LatLan. But sometimes you will get a meaningless 'name' for that specific LatLng, in that case, you can do a little modification for getting a meaningful 'name' as shown below.

Future<String> getLocationFromLatLng(LatLng latLng) async {
  List<Placemark> place = await placemarkFromCoordinates(latLng.latitude, latLng.longitude);
  int len = place.length;
  String name = '';
  for(int i = 0; i < len; i++){
    String temp = place[i].name ?? '';
    if(name.length <= temp.length){
      name = temp;
    }
  }
  return name;
}

Upvotes: 0

Saad Ahmed
Saad Ahmed

Reputation: 777

try this google instead on local

final coordinates = new Coordinates(position.latitude, position.longitude);  
geocoder.google('GOOGLE_API_KEY').findAddressesFromCoordinates(coordinates)

Upvotes: 1

Raouf Rahiche
Raouf Rahiche

Reputation: 31386

the findAddressesFromCoordinates method return a Future<List<Address>>

so you can do something like using then :

final coordinates = new Coordinates(latitude, longitude);
var addresses =  
Geocoder.local.findAddressesFromCoordinates(coordinates).then(
(data) => print(data);
);

or you can just use the async/await :

getAddressesFromCoordinates() async {
final coordinates = new Coordinates(1.10, 45.50);
addresses = await Geocoder.local.findAddressesFromCoordinates(coordinates);
first = addresses.first;
print("${first.featureName} : ${first.addressLine}");}

if you want to understand the basics of Asynchronous Programming in dart you should take a look at this article

Upvotes: 2

creativecreatorormaybenot
creativecreatorormaybenot

Reputation: 126854

findAddressesFromCoordinates returns a Future in this case.

You can either make your function or method async:

void yourFunction() async {
  final coordinates = new Coordinates(latitude, longitude);
  var addresses = await Geocoder.local.findAddressesFromCoordinates(coordinates);

  var first = addresses.first; 
  print("${first.featureName} : ${first.addressLine}");
}

In this case you need to use the await keyword in front of your method call, which will make the function only continue running after your object with the addresses has been retrieved.

The other option is using the then method on a Future, which would look like this:

void yourFunction() {
  final coordinates = new Coordinates(latitude, longitude);
  Geocoder.local.findAddressesFromCoordinates(coordinates).then((addresses) {
    var first = addresses.first; 
    print("${first.featureName} : ${first.addressLine}");
  });
}

In this case you are passing a callback to the then method, which will be executed with the result of findAddressesFromCoordinates in it once they are returned, but yourFunction itself will continue running.

Upvotes: 4

Related Questions