目次
はじめに
本記事は、「google_maps_flutter」のライブラリで取得した「LocationData」から住所を取得する方法です。
「google_maps_flutter」に関しては、以下の記事をご覧ください。
GPS座標(緯度、経度)から住所を取得する方法
「pubspec.yaml」に以下を追加します。
dependencies: ~~~省略~~~ geocoding: location: ~~~省略~~~
以下のクラスを作成します。
「getLocationDataAddress」メソッドを実行することで、コンソールに住所が取得できます。
import 'package:geocoding/geocoding.dart';
import 'package:location/location.dart';
class MapService {
/// コンストラクタ
MapService();
/// GPS座標(緯度、経度)から住所を取得する
void getLocationDataAddress(LocationData result) async {
try {
List<Placemark> placemark =
await placemarkFromCoordinates(result.latitude!, result.longitude!);
print(placemark);
print(placemark[0].name);
} catch (e) {
print('現在地を取得できません');
print(e);
}
}
}