diff --git a/lib/pages/home/widgets/map_widget.dart b/lib/pages/home/widgets/map_widget.dart index 399bbe5..def9850 100644 --- a/lib/pages/home/widgets/map_widget.dart +++ b/lib/pages/home/widgets/map_widget.dart @@ -7,8 +7,7 @@ import 'package:amap_flutter_map/amap_flutter_map.dart'; import 'package:autosos_flutter/const_config.dart'; import 'package:flutter/material.dart'; import 'package:permission_handler/permission_handler.dart'; - - +import 'package:amap_flutter_base/amap_flutter_base.dart'; class MapWidget extends StatefulWidget { const MapWidget({super.key}); @@ -19,11 +18,21 @@ class MapWidget extends StatefulWidget { class _MapWidgetState extends State { late AMapController _mapController; - late Map _locationResult; + Map _locationResult = {}; + late StreamSubscription> _locationListener; + final AMapFlutterLocation _locationPlugin = AMapFlutterLocation(); + + @override + void dispose() { + super.dispose(); + _locationListener.cancel(); + _locationPlugin.destroy(); + } @override void initState() { super.initState(); + /// 设置是否已经包含高德隐私政策并弹窗展示显示用户查看,如果未包含或者没有弹窗展示,高德定位SDK将不会工作 /// /// 高德SDK合规使用方案请参考官网地址:https://lbs.amap.com/news/sdkhgsy @@ -59,15 +68,15 @@ class _MapWidgetState extends State { ///Android: https://lbs.amap.com/api/android-location-sdk/guide/create-project/get-key /// ///iOS: https://lbs.amap.com/api/ios-location-sdk/guide/create-project/get-key - AMapFlutterLocation.setApiKey( - ConstConfig.androidKey, ConstConfig.iosKey); - + AMapFlutterLocation.setApiKey(ConstConfig.androidKey, ConstConfig.iosKey); ///iOS 获取native精度类型 if (Platform.isIOS) { requestAccuracyAuthorization(); } + _locationPlugin.setLocationOption(AMapLocationOption()); + ///注册定位结果监听 _locationListener = _locationPlugin .onLocationChanged() @@ -75,13 +84,15 @@ class _MapWidgetState extends State { setState(() { _locationResult = result; }); - print("locationRest=$_locationResult"); + print("location=$_locationResult"); + WidgetsBinding.instance.addPostFrameCallback((timeStamp) { + _moveCamera(_locationResult); + }); }); + _locationPlugin.startLocation(); + } - late StreamSubscription> _locationListener; - - final AMapFlutterLocation _locationPlugin = new AMapFlutterLocation(); @override Widget build(BuildContext context) { AMapWidget map = AMapWidget( @@ -126,15 +137,21 @@ class _MapWidgetState extends State { )); } + void _moveCamera(Map result) { + double lat = double.parse(result['latitude'] as String); + double lng = double.parse(result['longitude'] as String); + _mapController.moveCamera( + CameraUpdate.newCameraPosition(CameraPosition(target: LatLng(lat,lng)))); + } ///获取审图号 void getApprovalNumber() async { //普通地图审图号 String? mapContentApprovalNumber = - await _mapController.getMapContentApprovalNumber(); + await _mapController.getMapContentApprovalNumber(); //卫星地图审图号 String? satelliteImageApprovalNumber = - await _mapController.getSatelliteImageApprovalNumber(); + await _mapController.getSatelliteImageApprovalNumber(); print('地图审图号(普通地图): $mapContentApprovalNumber'); print('地图审图号(卫星地图): $satelliteImageApprovalNumber'); } @@ -160,7 +177,6 @@ class _MapWidgetState extends State { } else { //未授权则发起一次申请 status = await Permission.locationAlways.request(); - print("status=$status"); if (status == PermissionStatus.granted) { return true; } else { @@ -172,7 +188,7 @@ class _MapWidgetState extends State { ///获取iOS native的accuracyAuthorization类型 void requestAccuracyAuthorization() async { AMapAccuracyAuthorization currentAccuracyAuthorization = - await _locationPlugin.getSystemAccuracyAuthorization(); + await _locationPlugin.getSystemAccuracyAuthorization(); if (currentAccuracyAuthorization == AMapAccuracyAuthorization.AMapAccuracyAuthorizationFullAccuracy) { print("精确定位类型");