From ee11c80e9ff58950625c41fab8e706df259bb902 Mon Sep 17 00:00:00 2001 From: zhuce <821293938@qq.com> Date: Sat, 16 Mar 2024 18:11:34 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9C=A8=E5=9C=B0=E5=9B=BE=E4=B8=8A=E6=98=BE?= =?UTF-8?q?=E7=A4=BA=E5=AE=9A=E4=BD=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/pages/home/widgets/map_widget.dart | 44 ++++++++++++++++++-------- 1 file changed, 30 insertions(+), 14 deletions(-) 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("精确定位类型");