在地图上显示定位

main
zhuce 2024-03-16 18:11:34 +08:00
parent b4908ceb4c
commit ee11c80e9f
1 changed files with 30 additions and 14 deletions

View File

@ -7,8 +7,7 @@ import 'package:amap_flutter_map/amap_flutter_map.dart';
import 'package:autosos_flutter/const_config.dart'; import 'package:autosos_flutter/const_config.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart'; import 'package:permission_handler/permission_handler.dart';
import 'package:amap_flutter_base/amap_flutter_base.dart';
class MapWidget extends StatefulWidget { class MapWidget extends StatefulWidget {
const MapWidget({super.key}); const MapWidget({super.key});
@ -19,11 +18,21 @@ class MapWidget extends StatefulWidget {
class _MapWidgetState extends State<MapWidget> { class _MapWidgetState extends State<MapWidget> {
late AMapController _mapController; late AMapController _mapController;
late Map<String, Object> _locationResult; Map<String, Object> _locationResult = {};
late StreamSubscription<Map<String, Object>> _locationListener;
final AMapFlutterLocation _locationPlugin = AMapFlutterLocation();
@override
void dispose() {
super.dispose();
_locationListener.cancel();
_locationPlugin.destroy();
}
@override @override
void initState() { void initState() {
super.initState(); super.initState();
/// SDK /// SDK
/// ///
/// SDK使https://lbs.amap.com/news/sdkhgsy /// SDK使https://lbs.amap.com/news/sdkhgsy
@ -59,15 +68,15 @@ class _MapWidgetState extends State<MapWidget> {
///Android: https://lbs.amap.com/api/android-location-sdk/guide/create-project/get-key ///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 ///iOS: https://lbs.amap.com/api/ios-location-sdk/guide/create-project/get-key
AMapFlutterLocation.setApiKey( AMapFlutterLocation.setApiKey(ConstConfig.androidKey, ConstConfig.iosKey);
ConstConfig.androidKey, ConstConfig.iosKey);
///iOS native ///iOS native
if (Platform.isIOS) { if (Platform.isIOS) {
requestAccuracyAuthorization(); requestAccuracyAuthorization();
} }
_locationPlugin.setLocationOption(AMapLocationOption());
/// ///
_locationListener = _locationPlugin _locationListener = _locationPlugin
.onLocationChanged() .onLocationChanged()
@ -75,13 +84,15 @@ class _MapWidgetState extends State<MapWidget> {
setState(() { setState(() {
_locationResult = result; _locationResult = result;
}); });
print("locationRest=$_locationResult"); print("location=$_locationResult");
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
_moveCamera(_locationResult);
});
}); });
_locationPlugin.startLocation();
} }
late StreamSubscription<Map<String, Object>> _locationListener;
final AMapFlutterLocation _locationPlugin = new AMapFlutterLocation();
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
AMapWidget map = AMapWidget( AMapWidget map = AMapWidget(
@ -126,15 +137,21 @@ class _MapWidgetState extends State<MapWidget> {
)); ));
} }
void _moveCamera(Map<String,Object> 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 { void getApprovalNumber() async {
// //
String? mapContentApprovalNumber = String? mapContentApprovalNumber =
await _mapController.getMapContentApprovalNumber(); await _mapController.getMapContentApprovalNumber();
// //
String? satelliteImageApprovalNumber = String? satelliteImageApprovalNumber =
await _mapController.getSatelliteImageApprovalNumber(); await _mapController.getSatelliteImageApprovalNumber();
print('地图审图号(普通地图): $mapContentApprovalNumber'); print('地图审图号(普通地图): $mapContentApprovalNumber');
print('地图审图号(卫星地图): $satelliteImageApprovalNumber'); print('地图审图号(卫星地图): $satelliteImageApprovalNumber');
} }
@ -160,7 +177,6 @@ class _MapWidgetState extends State<MapWidget> {
} else { } else {
// //
status = await Permission.locationAlways.request(); status = await Permission.locationAlways.request();
print("status=$status");
if (status == PermissionStatus.granted) { if (status == PermissionStatus.granted) {
return true; return true;
} else { } else {
@ -172,7 +188,7 @@ class _MapWidgetState extends State<MapWidget> {
///iOS nativeaccuracyAuthorization ///iOS nativeaccuracyAuthorization
void requestAccuracyAuthorization() async { void requestAccuracyAuthorization() async {
AMapAccuracyAuthorization currentAccuracyAuthorization = AMapAccuracyAuthorization currentAccuracyAuthorization =
await _locationPlugin.getSystemAccuracyAuthorization(); await _locationPlugin.getSystemAccuracyAuthorization();
if (currentAccuracyAuthorization == if (currentAccuracyAuthorization ==
AMapAccuracyAuthorization.AMapAccuracyAuthorizationFullAccuracy) { AMapAccuracyAuthorization.AMapAccuracyAuthorizationFullAccuracy) {
print("精确定位类型"); print("精确定位类型");