Get full address details based on current location's latitude and longitude in Flutter

前端 未结 4 889
别跟我提以往
别跟我提以往 2021-01-12 17:39

I have used the location plugin in the flutter, I can get the latitude and longitude only. How to get the full ad

4条回答
  •  天命终不由人
    2021-01-12 18:17

    Using Geocoder plugin you can get address from the latitiude and longitude

     import 'package:location/location.dart';
     import 'package:geocoder/geocoder.dart';
     import 'package:flutter/services.dart';
    
    getUserLocation() async {//call this async method from whereever you need
    
          LocationData myLocation;
          String error;
          Location location = new Location();
          try {
            myLocation = await location.getLocation();
          } on PlatformException catch (e) {
            if (e.code == 'PERMISSION_DENIED') {
              error = 'please grant permission';
              print(error);
            }
            if (e.code == 'PERMISSION_DENIED_NEVER_ASK') {
              error = 'permission denied- please enable it from app settings';
              print(error);
            }
            myLocation = null;
          }
          currentLocation = myLocation;
          final coordinates = new Coordinates(
              myLocation.latitude, myLocation.longitude);
          var addresses = await Geocoder.local.findAddressesFromCoordinates(
              coordinates);
          var first = addresses.first;
          print(' ${first.locality}, ${first.adminArea},${first.subLocality}, ${first.subAdminArea},${first.addressLine}, ${first.featureName},${first.thoroughfare}, ${first.subThoroughfare}');
          return first;
        }
    

提交回复
热议问题