fix:修复android平台无法显示地图和定位信息问题

This commit is contained in:
Liuyf 2025-01-13 16:52:21 +08:00
parent 5e818c9d67
commit 4d6ca7ac20
2 changed files with 116 additions and 146 deletions

View File

@ -1,4 +1,3 @@
import 'dart:async';
import 'dart:io';
@ -31,11 +30,13 @@ class LockAddressGaoDePage extends StatefulWidget {
class _LockAddressGaoDePageState extends State<LockAddressGaoDePage>
with RouteAware {
final LockAddressGaoDeLogic logic = Get.put(LockAddressGaoDeLogic());
final LockAddressGaoDeState state = Get.find<LockAddressGaoDeLogic>().state;
final LockAddressGaoDeState state = Get
.find<LockAddressGaoDeLogic>()
.state;
//
static AMapApiKey amapApiKeys =
AMapApiKey(iosKey: F.aMapKey.iosKey, androidKey: F.aMapKey.androidKey);
AMapApiKey(iosKey: F.aMapKey.iosKey, androidKey: F.aMapKey.androidKey);
AMapController? mapController;
AMapFlutterLocation location = AMapFlutterLocation();
@ -52,51 +53,16 @@ class _LockAddressGaoDePageState extends State<LockAddressGaoDePage>
requestPermission();
}
// Future<void> requestPermission() async {
// final status = await Permission.location.request();
// AppLog.log("Permission.location.request()=status:$status");
// permissionStatus = status;
// if(Platform.isIOS){
// _setLocationOption();
// requestIOSLocation();
// }
// switch (status) {
// case PermissionStatus.denied:
// // AppLog.log("拒绝");
// break;
// case PermissionStatus.granted:
// if(Platform.isIOS){
// // _setLocationOption();
// // requestIOSLocation();
// }else{
// requestAndroidLocation();
// location.startLocation();
// }
// break;
// case PermissionStatus.limited:
// // AppLog.log("限制");
// break;
// case PermissionStatus.permanentlyDenied:
// // AppLog.log("永久的否认");
// break;
// case PermissionStatus.provisional:
// // AppLog.log("临时");
// break;
// default:
// // AppLog.log("其他状态");
// // requestLocation();
// break;
// }
// }
Future<void> requestPermission() async {
final bool status = await PermissionDialog.request(Permission.location);
if(Platform.isIOS){
if (Platform.isIOS) {
_setLocationOption();
requestIOSLocation();
}
if (!Platform.isIOS || status) {
_setLocationOption();
requestAndroidLocation();
location.startLocation();
}
@ -114,19 +80,18 @@ class _LockAddressGaoDePageState extends State<LockAddressGaoDePage>
}
Future<void> requestIOSLocation() async {
location = AMapFlutterLocation()
..setLocationOption(AMapLocationOption())
..onLocationChanged().listen((Map<String, Object> event) {
// AppLog.log("listenLocationChanged$event");
// EasyLoading.dismiss();
if (event.isNotEmpty) {
setState(() {
addressInfo = event;
});
// location.stopLocation();
}
})
..startLocation();
location.setLocationOption(AMapLocationOption());
location.onLocationChanged().listen((Map<String, Object> event) {
// AppLog.log("listenLocationChanged$event");
// EasyLoading.dismiss();
if (event.isNotEmpty) {
setState(() {
addressInfo = event;
});
// location.stopLocation();
}
});
location.startLocation();
}
@override
@ -152,85 +117,88 @@ class _LockAddressGaoDePageState extends State<LockAddressGaoDePage>
SizedBox(
child: (addressInfo != null && addressInfo!.containsKey('latitude'))
? Column(
children: <Widget>[
SizedBox(
height: 1.sw / 5 * 4,
width: 1.sw,
child: AMapWidget(
apiKey: amapApiKeys,
//
initialCameraPosition: CameraPosition(
target: LatLng(
double.parse(
addressInfo!['latitude'].toString()),
double.parse(
addressInfo!['longitude'].toString())),
zoom: 10.0,
),
//
myLocationStyleOptions: MyLocationStyleOptions(
true,
),
// normal,satellite,night, navi,bus,
mapType: MapType.normal,
//
minMaxZoomPreference:
const MinMaxZoomPreference(3, 20),
//
privacyStatement: const AMapPrivacyStatement(
hasAgree: true, hasContains: true, hasShow: true),
// AMapController
onMapCreated: (AMapController controller) {
mapController = controller;
},
onLocationChanged: (AMapLocation location) {
print("onLocationChanged ${location}");
},
),
),
Container(
// color: Colors.red,
margin:
EdgeInsets.only(left: 25.w, top: 20.h, right: 25.w),
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
SizedBox(
height: 1.sw / 5 * 4,
width: 1.sw,
child: AMapWidget(
apiKey: amapApiKeys,
//
initialCameraPosition: CameraPosition(
target: LatLng(
double.parse(
addressInfo!['latitude'].toString()),
double.parse(
addressInfo!['longitude'].toString())),
zoom: 10.0,
),
//
myLocationStyleOptions: MyLocationStyleOptions(
true,
),
// normal,satellite,night, navi,bus,
mapType: MapType.normal,
//
minMaxZoomPreference:
const MinMaxZoomPreference(3, 20),
//
privacyStatement: const AMapPrivacyStatement(
hasAgree: true, hasContains: true, hasShow: true),
// AMapController
onMapCreated: (AMapController controller) {
mapController = controller;
},
),
),
Container(
// color: Colors.red,
margin:
EdgeInsets.only(left: 25.w, top: 20.h, right: 25.w),
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
Flexible(
child: Text('检查以确保以下地址是正确的'.tr,
style: TextStyle(fontSize: 24.sp))),
],
),
),
// SizedBox(height: 20.h),
Container(
// color: Colors.red,
// height: 45.h,
margin: EdgeInsets.only(
left: 25.w, top: 20.h, right: 25.w),
child: Column(
children: <Widget>[
Row(
children: <Widget>[
Flexible(
child: Text('检查以确保以下地址是正确的'.tr,
style: TextStyle(fontSize: 24.sp))),
Expanded(
child: Text(
addressInfo!['address'].toString() ??
'',
style: const TextStyle(
color: Colors.grey,
fontSize: 16,
fontWeight: FontWeight.w500,
overflow: TextOverflow.clip))),
],
),
),
// SizedBox(height: 20.h),
Container(
// color: Colors.red,
// height: 45.h,
margin: EdgeInsets.only(
left: 25.w, top: 20.h, right: 25.w),
child: Column(
children: <Widget>[
Row(
children: <Widget>[
Expanded(
child: Text(
addressInfo!['address'].toString() ??
'',
style: const TextStyle(
color: Colors.grey,
fontSize: 16,
fontWeight: FontWeight.w500,
overflow: TextOverflow.clip))),
],
),
SizedBox(height: 5.h),
Container(
height: 1.h,
color: AppColors.mainColor,
),
],
)),
],
)
SizedBox(height: 5.h),
Container(
height: 1.h,
color: AppColors.mainColor,
),
],
)),
],
)
: SizedBox(
height: 1.sw / 5 * 4 + 65.h * 2,
child: Center(child: Text('地图加载中,请稍候。。。'.tr))),
height: 1.sw / 5 * 4 + 65.h * 2,
child: Center(child: Text('地图加载中,请稍候。。。'.tr))),
),
SizedBox(height: 200.h),
Row(
@ -242,14 +210,15 @@ class _LockAddressGaoDePageState extends State<LockAddressGaoDePage>
style: TextStyle(color: Colors.black, fontSize: 24.sp),
),
onPressed: () {
Get.toNamed(Routers.addLockSelectCountryPage, arguments: <String, Object>{
'addressInfo': {},
'pwdTimestamp': state.pwdTimestamp.value,
'lockInfo': state.lockInfo,
'featureValue': state.featureValue,
'featureSettingValue': state.featureSettingValue,
'featureSettingParams': state.featureSettingParams,
});
Get.toNamed(Routers.addLockSelectCountryPage,
arguments: <String, Object>{
'addressInfo': {},
'pwdTimestamp': state.pwdTimestamp.value,
'lockInfo': state.lockInfo,
'featureValue': state.featureValue,
'featureSettingValue': state.featureSettingValue,
'featureSettingParams': state.featureSettingParams,
});
},
),
TextButton(
@ -262,15 +231,16 @@ class _LockAddressGaoDePageState extends State<LockAddressGaoDePage>
logic.showToast('还未获取到位置信息哦,请耐心等待一下!'.tr);
return;
}
Get.toNamed(Routers.saveLockPage, arguments: <String, Object?>{
'addressInfo': addressInfo,
'pwdTimestamp': state.pwdTimestamp.value,
'lockInfo': state.lockInfo,
'featureValue': state.featureValue,
'featureSettingValue': state.featureSettingValue,
'featureSettingParams': state.featureSettingParams,
'isFromMap': 1,
});
Get.toNamed(Routers.saveLockPage,
arguments: <String, Object?>{
'addressInfo': addressInfo,
'pwdTimestamp': state.pwdTimestamp.value,
'lockInfo': state.lockInfo,
'featureValue': state.featureValue,
'featureSettingValue': state.featureSettingValue,
'featureSettingParams': state.featureSettingParams,
'isFromMap': 1,
});
// Navigator.pushNamed(context, Routers.saveLockPage);
},
),

View File

@ -193,11 +193,11 @@ dependencies:
easy_refresh: ^3.3.4
#高德地图定位
amap_flutter_location: ^3.0.0
amap_flutter_location: 3.0.0
#权限使用
permission_handler: ^11.3.1
#高德地图地图
amap_flutter_map: ^3.0.0
amap_flutter_map: 3.0.0
# 谷歌地图
google_maps_flutter: ^2.2.5