Position? position;
List<Placemark>? placeMarks;
getCurrentLocation() async
{
Position newPosition = await Geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.high, //exact location use high
);
position = newPosition; //get longitude and latitude
placeMarks = await placemarkFromCoordinates(
position!.latitude,
position!.longitude,
);
Placemark pMark = placeMarks![0];
String completeAddress = '${pMark.subThoroughfare} ${pMark.thoroughfare}, '
'${pMark.subLocality} ${pMark.locality}, ${pMark.subAdministrativeArea}, '
'${pMark.administrativeArea} ${pMark.postalCode}, ${pMark.country}';
location.text = completeAddress;
}
child: ElevatedButton.icon(
onPressed: (){
getCurrentLocation();
},
icon: const Icon(
Icons.location_on,
我没有在我的Android Studio错误,但当我点击按钮获取当前位置,它没有出现我的当前位置.
1条答案
按热度按时间h7appiyu1#
您还需要向用户请求位置权限,permission_handler包允许您使用系统对话框来请求权限:
如果权限已经被授予,它将不会再次询问用户,但会返回true。它还允许您检查位置是否打开:
在请求
locationAlways
之前,必须先请求locationWhenInUse
权限。(All示例改编自程序包README。)