在地图上显示定位
parent
b4908ceb4c
commit
ee11c80e9f
|
@ -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,6 +137,12 @@ 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 {
|
||||||
|
@ -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 {
|
||||||
|
|
Loading…
Reference in New Issue