import 'dart:async'; import 'package:flutter/material.dart'; import 'package:amap_flutter_location/amap_flutter_location.dart'; import 'package:amap_flutter_location/amap_location_option.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:permission_handler/permission_handler.dart'; import 'package:amap_flutter_map/amap_flutter_map.dart'; import 'package:amap_flutter_base/amap_flutter_base.dart'; import '../../../app_settings/app_colors.dart'; typedef BlockAddressMapCallback = void Function(dynamic addressMap); class LockAddressGaoDePage extends StatefulWidget { BlockAddressMapCallback callback; LockAddressGaoDePage({Key? key, required this.callback}) : super(key: key); @override State createState() => _LockAddressGaoDePageState(); } class _LockAddressGaoDePageState extends State{ // 高德地图 static const AMapApiKey amapApiKeys = AMapApiKey(iosKey: '883a3355d2d77c2fdc2667030dc97ffe', androidKey: '11d49b3f4fc09c04a02bbb7500925ba2'); AMapController? mapController; AMapFlutterLocation? location; PermissionStatus? permissionStatus; Map? addressInfo; @override void initState() { super.initState(); AMapFlutterLocation.setApiKey("11d49b3f4fc09c04a02bbb7500925ba2", "883a3355d2d77c2fdc2667030dc97ffe"); AMapFlutterLocation.updatePrivacyAgree(true); AMapFlutterLocation.updatePrivacyShow(true, true); requestPermission(); } Future requestPermission() async { final status = await Permission.location.request(); permissionStatus = status; switch (status) { case PermissionStatus.denied: print("拒绝"); break; case PermissionStatus.granted: requestLocation(); break; case PermissionStatus.limited: print("限制"); break; default: print("其他状态"); requestLocation(); break; } } void requestLocation() { location = AMapFlutterLocation() ..setLocationOption(AMapLocationOption()) ..onLocationChanged().listen((event) { print("listenLocationChanged$event"); double? latitude = double.parse(event['latitude'] as String); double? longitude = double.parse(event['longitude'] as String); if (latitude != 0 && longitude != 0) { widget.callback(event); setState(() { addressInfo = event; // currentLocation = CameraPosition( // target: LatLng(latitude, longitude), // zoom: 10, // ); }); } }) ..startLocation(); } @override Widget build(BuildContext context) { if (addressInfo?['latitude'] == null || addressInfo?['longitude'] == null || addressInfo?['errorCode'] == 0) { return const Center(child: Text('地图加载中,请稍候。。。')); } var latitude = 39.909187; var longitude = 116.397451; if(addressInfo!.containsKey('latitude')){ latitude = double.parse(addressInfo!['latitude'] as String); longitude = double.parse(addressInfo!['longitude'] as String); } return Stack( children: [ Container( height: MediaQuery.of(context).size.height, width: MediaQuery.of(context).size.width, child: (addressInfo!["address"].toString().isNotEmpty) ? AMapWidget( apiKey: amapApiKeys, // 初始化地图中心 initialCameraPosition: ( CameraPosition( target: LatLng(latitude, longitude), 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(), ), Positioned( left: 20.w, right: 20.w, bottom: 100.h, child: Container( // height: h(106), decoration: BoxDecoration( color: AppColors.mainColor, borderRadius: BorderRadius.circular(15.w), ), child:Column( children: [ Container( // height: h(53), padding: EdgeInsets.only(top: 15.h, bottom: 15, left:15.w, right: 15.w), child: Row( children: [ Image.asset('images/main/icon_addUserShowAddress.png', width: 30.w, height: 30.w), SizedBox(width: 10.w), Expanded( child: Text(addressInfo!["address"].toString() ?? "", style: const TextStyle(color: Colors.white, fontSize: 16, fontWeight: FontWeight.w500, overflow: TextOverflow.clip)) ), ], ), ), // Container(height: 1.h, color: const Color(0xFF021732),), // Container( // // height: h(52), // padding: EdgeInsets.only(top: 15.h, bottom: 15, left:15.w, right: 15.w), // child: Row( // children: [ // Image.asset('images/main/icon_addUserAddressShowTime.png', width: 30.w, height: 30.w), // SizedBox(width: 10.w,), // Expanded( // child: Text(DateTool().getNowDateYMDHM(), style: const TextStyle(color: Colors.white, fontSize: 16, fontWeight: FontWeight.w500),) // ), // ], // ), // ), ], ) ),), ], ); } @override void dispose() { super.dispose(); location?.destroy(); print("地图界面销毁了"); } }