Dilrukshika Rajapakshe
Dilrukshika Rajapakshe

Reputation: 113

when I try to get address using geocoder, given a run time error of "Unhandled Exception: PlatformException(failed, Failed, null)"

I try to get the location using dart plugin of "geolocation and geocode" using. but the code gives an error of "Unhandled Exception: PlatformException(failed, Failed, null)". how do I overcome it?

import 'package:geolocator/geolocator.dart';
import 'package:geocoder/geocoder.dart';

Future<String> getCurrentLocation() async {
  final location = await Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
  String latitude =(location.latitude).toString();
  String longitude = (location.longitude).toString();
  String _center = latitude + "," + longitude;
  print(_center);
  final coordinates = await new Coordinates(location.latitude, location.longitude);
  var addresses = await Geocoder.local.findAddressesFromCoordinates(coordinates);

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

Upvotes: 0

Views: 3972

Answers (2)

Artur
Artur

Reputation: 857

You need to create a key in Google Cloud Platform and enable the Geocoder API for it

Use:

await Geocoder.google(your_API_Key).findAddressesFromCoordinates(coordinates);

Instead of:

await Geocoder.local.findAddressesFromCoordinates(coordinates);

Upvotes: 0

Sathya Prakash
Sathya Prakash

Reputation: 56

Add your apikey and it will work:

 var addresses = await Geocoder.google ( '<---------YOUR APIKEY-------->' ).findAddressesFromCoordinates(coordinates);

Upvotes: 4

Related Questions