[Flutter]位置情報を取得するlocation
Add this to your package's pubspec.yaml file:
dependencies:
location: ^3.0.0
Android
特に設定は必要ない
iOS
ios/Runner/Info.plistに以下の項目を追加
<key>NSLocationAlwaysUsageDescription</key>
<string>Your location is required for this app</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Your location is required for this app</string>
スマホの位置情報がオンになっているか、アプリが位置情報にアクセスできるかの確認
Future<bool> checkLocation() async {
Location location = new Location();
bool _serviceEnabled;
PermissionStatus _permissionGranted;
_serviceEnabled = await location.serviceEnabled();
if (!_serviceEnabled) {
_serviceEnabled = await location.requestService();
if (!_serviceEnabled) {
return false;
}
}
_permissionGranted = await location.hasPermission();
if (_permissionGranted == PermissionStatus.denied) {
_permissionGranted = await location.requestPermission();
if (_permissionGranted != PermissionStatus.granted) {
return false;
}
}
return true;
}
情報取得
Future<List<String>> getLocation() async {
Location location = new Location();
LocationData _locationData = await location.getLocation();
return [
_locationData.longitude.toString(),
_locationData.latitude.toString()
];
}
参考