在地图上显示定位

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: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<MapWidget> {
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
void initState() {
super.initState();
/// SDK
///
/// 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
///
///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<MapWidget> {
setState(() {
_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
Widget build(BuildContext context) {
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 {
//
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<MapWidget> {
} else {
//
status = await Permission.locationAlways.request();
print("status=$status");
if (status == PermissionStatus.granted) {
return true;
} else {
@ -172,7 +188,7 @@ class _MapWidgetState extends State<MapWidget> {
///iOS nativeaccuracyAuthorization
void requestAccuracyAuthorization() async {
AMapAccuracyAuthorization currentAccuracyAuthorization =
await _locationPlugin.getSystemAccuracyAuthorization();
await _locationPlugin.getSystemAccuracyAuthorization();
if (currentAccuracyAuthorization ==
AMapAccuracyAuthorization.AMapAccuracyAuthorizationFullAccuracy) {
print("精确定位类型");