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

前端 未结 4 840
别跟我提以往
别跟我提以往 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:10
    `
    import 'package:flutter/material.dart';
    import 'package:geocoder/geocoder.dart';
    import 'package:geolocator/geolocator.dart';
    
    _getLocation() async {
    GeolocationStatus geolocationStatus = await 
    Geolocator().checkGeolocationPermissionStatus();
    
    Position position = await Geolocator()
        .getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
    debugPrint('location: ${position.latitude}');
    
    
    final coordinates = new Coordinates(position.latitude, position.longitude);
    debugPrint('coordinates is: $coordinates');
    
    var addresses =
        await Geocoder.local.findAddressesFromCoordinates(coordinates);
    var first = addresses.first;
    // print number of retured addresses 
    debugPrint('${addresses.length}');
    // print the best address
    debugPrint("${first.featureName} : ${first.addressLine}");
    //print other address names
    debugPrint(Country:${first.countryName} AdminArea:${first.adminArea} SubAdminArea:${first.subAdminArea}");
    //print more address names
    debugPrint(Locality:${first.locality}: Sublocality:${first.subLocality}");
    }
    

    `

    0 讨论(0)
  • 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;
        }
    
    0 讨论(0)
  • 2021-01-12 18:21

    in pubspec.yaml

    geolocator: '^5.1.1'
      geocoder: ^0.2.1
    

    Import this packages

    import 'package:geolocator/geolocator.dart';
    import 'package:geocoder/geocoder.dart';
    
    
    _getLocation() async
          {
            Position position = await Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
            debugPrint('location: ${position.latitude}');
            final coordinates = new Coordinates(position.latitude, position.longitude);
            var addresses = await Geocoder.local.findAddressesFromCoordinates(coordinates);
            var first = addresses.first;
            print("${first.featureName} : ${first.addressLine}");
          }
    
    0 讨论(0)
  • 2021-01-12 18:27

    You'll need to use a "reverse geocoding" service, such as Google's API: https://developers.google.com/maps/documentation/geocoding/start. There are others out there as well... just google for "geocoding".

    Update: apparently there's a local API as well. See: https://pub.dartlang.org/packages/geocoder

    0 讨论(0)
提交回复
热议问题