flutter-geolocator icon indicating copy to clipboard operation
flutter-geolocator copied to clipboard

Unhandled Exception: PlatformException(ERROR_GEOCODING_INVALID_COORDINATES, Unable to find an address for the supplied coordinates., null)

Open mtkgeek opened this issue 7 years ago • 5 comments

💬 Questions and Help

I have been getting the above error whenever i try geocoding, here's my code. Just to be sure i do get my co-ordinates(print statements confirms that)

getUserLocation() async {
    Position position = await Geolocator()
        .getCurrentPosition();
        print(position.latitude);
        print(position.longitude);
    List<Placemark> placemarks = await Geolocator()
        .placemarkFromCoordinates(position.latitude, position.longitude);
        print(placemarks);
    Placemark placemark = placemarks[0];
    print(placemark);
    String completeAddress =
        '${placemark.subThoroughfare} ${placemark.thoroughfare}, ${placemark.subLocality} ${placemark.locality}, ${placemark.subAdministrativeArea}, ${placemark.administrativeArea} ${placemark.postalCode}, ${placemark.country}';
    print(completeAddress);
    String formattedAddress = "${placemark.locality}, ${placemark.country}";
    var text = formattedAddress;
  }

The code doesn't get past the print longitude statement and i get the above error

mtkgeek avatar Dec 01 '19 17:12 mtkgeek

Aqui está o problema

no arquivo Geolocator (5.1.5), na linha 205

está desta formar

void _handleInvalidPermissions(PermissionStatus permission) { if (permission == PermissionStatus.denied) { throw PlatformException( code: 'PERMISSION_DENIED', message: 'Access to location data denied', details: null); } } o arquivo está retornando uma Exception, quando deveria retorna ou NULL ou a própria permission. segue arquivo corrigido

_handleInvalidPermissions(PermissionStatus permission) { if (permission == PermissionStatus.denied) { return permission; } }

hevertonpablo avatar Dec 05 '19 16:12 hevertonpablo

you have to put your request inside a try-catch block like this:

getUserLocation() async {
    try {
    Position position = await Geolocator()
        .getCurrentPosition();
        print(position.latitude);
        print(position.longitude);
    List<Placemark> placemarks = await Geolocator()
        .placemarkFromCoordinates(position.latitude, position.longitude);
        print(placemarks);
    Placemark placemark = placemarks[0];
    print(placemark);
    String completeAddress =
        '${placemark.subThoroughfare} ${placemark.thoroughfare}, ${placemark.subLocality} ${placemark.locality}, ${placemark.subAdministrativeArea}, ${placemark.administrativeArea} ${placemark.postalCode}, ${placemark.country}';
    print(completeAddress);
    String formattedAddress = "${placemark.locality}, ${placemark.country}";
    var text = formattedAddress;
}
catch (e) {
   //handle errors and exceptions here
}
  }

because this lib return an exception in case of no results (rather than an empty list, which would make more sense) And unfortunatelly you can't use the then().catchError() pattern either because this is an exception that is thrown by this lib in case of no results and not an error (so it isn't catched by the Future.catchError() pattern meaning you necessarilly need to use try{}catch(e){} to handle the exception )

ramsestom avatar Dec 06 '19 05:12 ramsestom

Aqui está o problema

no arquivo Geolocator (5.1.5), na linha 205

está desta formar

void _handleInvalidPermissions(PermissionStatus permission) { if (permission == PermissionStatus.denied) { throw PlatformException( code: 'PERMISSION_DENIED', message: 'Access to location data denied', details: null); } } o arquivo está retornando uma Exception, quando deveria retorna ou NULL ou a própria permission. segue arquivo corrigido

_handleInvalidPermissions(PermissionStatus permission) { if (permission == PermissionStatus.denied) { return permission; } }

Compiler message: /C:/scr/flutter/.pub-cache/hosted/pub.dartlang.org/geolocator-5.1.5/lib/geolocator.dart:216:14: Error: Can't return a value from a void function. return permission;; ^ Estava na maior esperança que sua tese estava correta, mas o compiler não pensa assim :( Mas foi ótima sua contribuição.

SirJulian avatar Dec 06 '19 08:12 SirJulian

Aqui está o problema no arquivo Geolocator (5.1.5), na linha 205 está desta formar void _handleInvalidPermissions(PermissionStatus permission) { if (permission == PermissionStatus.denied) { throw PlatformException( code: 'PERMISSION_DENIED', message: 'Access to location data denied', details: null); } } o arquivo está retornando uma Exception, quando deveria retorna ou NULL ou a própria permission. segue arquivo corrigido _handleInvalidPermissions(PermissionStatus permission) { if (permission == PermissionStatus.denied) { return permission; } }

Compiler message: /C:/scr/flutter/.pub-cache/hosted/pub.dartlang.org/geolocator-5.1.5/lib/geolocator.dart:216:14: Error: Can't return a value from a void function. return permission;; ^ Estava na maior esperança que sua tese estava correta, mas o compiler não pensa assim :( Mas foi ótima sua contribuição.

Funciona sim amigo, o o seu problema é que deixou o void na frente do _handleInvalidPermissions e não pode. Pois o void significa que não retorna nada, como você está retornando alguma coisa, não pode deixar o void.

Da uma olhada no meu código novamente, percebe que não tem o void?

hevertonpablo avatar Dec 06 '19 10:12 hevertonpablo

Since version 6.0.0 the geocoding features are no longer part of the geolocator plugin and moved to their own plugin which can be found here.

mvanbeusekom avatar Sep 04 '20 15:09 mvanbeusekom