import 'package:amap_map_fluttify/amap_map_fluttify.dart'; import 'package:amap_search_fluttify/amap_search_fluttify.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:liftmanager/utils/fast_notification.dart'; import 'package:liftmanager/utils/location_helper.dart'; import 'package:liftmanager/utils/toast.dart'; import 'package:liftmanager/widgets/app_bar.dart'; import 'package:permission_handler/permission_handler.dart'; import '../../../main.dart'; import '../../../utils/toast.dart'; /** * 地图选择点控件 */ class MyHomePage extends StatefulWidget { /** * 选择点后回调事件 */ // final Function onChoicePoint; // MyHomePage(this.onChoicePoint); MyHomePage(this.type); final String type; @override _MapChoicePointState createState() => _MapChoicePointState(); } class _MapChoicePointState extends State with SingleTickerProviderStateMixin { //----属性---- //地图控制器 AmapController _amapController; //选择的点 Marker _markerSelect; //搜索出来之后选择的点 Marker _markerSeached; //所在城市 String city; //搜索框文字控制器 TextEditingController _serachController; //自定义marker点图标图片路径 Uri _imgUri = Uri.parse('images/icon_star_selected.png'); //----方法---- /** * 获取权限 */ Future _requestPermission() async { final permissions = await PermissionHandler() .requestPermissions([PermissionGroup.location]); if (permissions[PermissionGroup.location] == PermissionStatus.granted) { return true; } else { // toasts('需要定位权限!'); return false; } } setlatlng() { var helper = LocationHelper(); helper .getLocation((String lat, String lng, String cityName, {provinceName}) { if (lat != null && lng != null) { helper.stopGettingLocation(); } var latLng = LatLng(double.parse(lat), double.parse(lng)); _amapController?.setCenterCoordinate(latLng); // setState(() {}); // NavigatorUtils.goBackWithParams(context, "$lng,$lat"); }); } /** * 根据搜索条件选出想要的点 */ Future _openModalBottomSheet() async { //收起键盘 print("----------"); FocusScope.of(context).requestFocus(FocusNode()); //根据关键字及城市进行搜索 final poiList = await AmapSearch.searchKeyword( _serachController.text, city: city, ); List points = []; //便利拼接信息 for (var item in poiList) { points.add({ 'title': item.title, 'address': item.adName + item.address, 'position': item.latLng, }); } //弹出底部对话框并等待选择 final option = await showModalBottomSheet( context: context, builder: (BuildContext context) { return points.length > 0 ? ListView.builder( itemCount: points.length, itemBuilder: (BuildContext itemContext, int i) { return ListTile( title: Text(points[i]['title']), subtitle: Text(points[i]['address']), onTap: () { Navigator.pop(context, points[i]); print(points[i]); print(4444444); toCenter(points[i]); }, ); }, ) : Container( alignment: Alignment.center, padding: EdgeInsets.all(40), child: Text('暂无数据'), ); }, ); if (option != null) { LatLng selectlatlng = option['position']; //将地图中心点移动到选择的点 _amapController.setCenterCoordinate( LatLng(selectlatlng.latitude, selectlatlng.longitude)); //删除原来地图上搜索出来的点 if (_markerSeached != null) { _markerSeached.remove(); } //将搜索出来的点显示在界面上 --此处不能使用自定义图标的marker,使用会报错,至今也没有解决 _markerSeached = await _amapController.addMarker(MarkerOption( latLng: selectlatlng, )); ReGeocode reGeocodeList = await AmapSearch.searchReGeocode( selectlatlng, ); // print(await reGeocodeList.toFutureString()); // print(await reGeocodeList.provinceName); // print(await reGeocodeList.cityName); // print(await reGeocodeList.districtName); print("reGeocodeList.provinceName================="); var addressM = (await reGeocodeList.provinceName) + (await reGeocodeList.cityName) + (await reGeocodeList.districtName); var address = await reGeocodeList.formatAddress; showAlert( navigatorKey.currentState.overlay.context, "提示", "出诊地址为: $address", "确定", () { Navigator.of(navigatorKey.currentState.overlay.context)..pop(); if (context != null) { Navigator.of(navigatorKey.currentState.overlay.context)..pop(); } // setExtAddress(address+","+coord.latitude.toString()+","+coord.longitude.toString()); String setAddress = address + "," + selectlatlng.latitude.toString() + "," + selectlatlng.longitude.toString(); String setAddressMaster = addressM; if (widget.type == "1") { FastNotification.push("set_address", setAddress); } else if (widget.type == "2") { FastNotification.push("set_address_master", setAddressMaster); } }, txt2: "取消", onPre2: () { Navigator.of(navigatorKey.currentState.overlay.context)..pop(); }, ); } } Future toCenter(option) async { print(option['position']); print("======================"); LatLng selectlatlng = option['position']; //将地图中心点移动到选择的点 _amapController.setCenterCoordinate( LatLng(selectlatlng.latitude, selectlatlng.longitude)); //删除原来地图上搜索出来的点 if (_markerSeached != null) { _markerSeached.remove(); } //将搜索出来的点显示在界面上 --此处不能使用自定义图标的marker,使用会报错,至今也没有解决 _markerSeached = await _amapController.addMarker(MarkerOption( latLng: selectlatlng, )); } @override void initState() { super.initState(); _serachController = TextEditingController(); _requestPermission(); } @override void dispose() { super.dispose(); } @override Widget build(BuildContext context) { return Scaffold( appBar: MyAppBar( centerTitle: "地图选择", ), body: Stack( alignment: AlignmentDirectional.topCenter, children: [ AmapView( // 地图类型 (可选) mapType: MapType.Standard, // 是否显示缩放控件 (可选) // showZoomControl: true, // 是否显示指南针控件 (可选) showCompass: true, // 是否显示比例尺控件 (可选) showScaleControl: true, // 是否使能缩放手势 (可选) zoomGesturesEnabled: true, // 是否使能滚动手势 (可选) scrollGesturesEnabled: true, // 是否使能旋转手势 (可选) rotateGestureEnabled: false, // 是否使能倾斜手势 (可选) tiltGestureEnabled: false, // 缩放级别 (可选) zoomLevel: 16, // 中心点坐标 (可选) // centerCoordinate: LatLng(39, 116), // 标记 (可选) markers: [], // 标识点击回调 (可选) onMarkerClicked: (Marker marker) async { if (_markerSeached == null) { return; } //获取点击点的位置 var location = await marker.location; var lon = location.longitude; var lat = location.latitude; //获取搜索点的位置 var slocation = await _markerSeached.location; var slon = slocation.longitude; var slat = slocation.latitude; //比较位置 if (lon == slon && lat == slat) { //移除原来的点 if (_markerSeached != null) { _markerSeached.remove(); } if (_markerSelect != null) { _markerSelect.remove(); } //画上新的点 _markerSelect = await _amapController.addMarker( MarkerOption( latLng: location, // iconUri: _imgUri, // imageConfig: createLocalImageConfiguration(context), width: 64, height: 64, anchorV: 0.7, anchorU: 0.5, ), ); } }, // 地图点击回调 (可选) onMapClicked: (LatLng coord) async { print("//////////////"); print(coord.latitude); print(coord.longitude); String address; String addressM; /// 逆地理编码(坐标转地址) ReGeocode reGeocodeList = await AmapSearch.searchReGeocode( coord, ); // print(await reGeocodeList.toFutureString()); // print(await reGeocodeList.provinceName); // print(await reGeocodeList.cityName); // print(await reGeocodeList.districtName); print("reGeocodeList.provinceName================="); addressM = (await reGeocodeList.provinceName) + (await reGeocodeList.cityName) + (await reGeocodeList.districtName); address = await reGeocodeList.formatAddress; print(address); print(address + "," + coord.latitude.toString() + "," + coord.longitude.toString()); if (_amapController != null) { //移除原来的点 if (_markerSelect != null) { _markerSelect.remove(); } if (_markerSeached != null) { _markerSeached.remove(); } _markerSeached = await _amapController.addMarker(MarkerOption( latLng: coord, )); //画上新的点 // _markerSelect = await _amapController.addMarker(MarkerOption( // latLng: coord, // iconUri: _imgUri, // imageConfig: createLocalImageConfiguration(context), // width: 64, // height: 64, // anchorV: 0.7, // anchorU: 0.5)); // widget.onChoicePoint(coord); showAlert( navigatorKey.currentState.overlay.context, "提示", "出诊地址为: $address", "确定", () { Navigator.of(navigatorKey.currentState.overlay.context) ..pop(); if (context != null) { Navigator.of(navigatorKey.currentState.overlay.context) ..pop(); } // setExtAddress(address+","+coord.latitude.toString()+","+coord.longitude.toString()); String setAddress = address + "," + coord.latitude.toString() + "," + coord.longitude.toString(); String setAddressMaster = addressM; if (widget.type == "1") { FastNotification.push("set_address", setAddress); } else if (widget.type == "2") { FastNotification.push( "set_address_master", setAddressMaster); } }, txt2: "取消", onPre2: () { Navigator.of(navigatorKey.currentState.overlay.context) ..pop(); }, ); } }, onMapMoveStart: (MapMove move) {}, // 地图创建完成回调 (可选) onMapCreated: (controller) async { _amapController = controller; //申请权限 if (await _requestPermission()) { //获取所在城市 print(123456789); // final location = await AmapLocation.fetchLocation(); // city = await location.city; // print(location); // print(city); setlatlng(); print(1111111); //显示自己的定位 await controller.showMyLocation(MyLocationOption()); // await initSerach(); } }, ), Container( margin: EdgeInsets.all(20), width: MediaQuery.of(context).size.width, height: 46, decoration: BoxDecoration(color: Colors.white), child: Row( mainAxisAlignment: MainAxisAlignment.spaceAround, children: [ Container( padding: EdgeInsets.all(8), width: MediaQuery.of(context).size.width - 20 - 80, child: TextField( controller: _serachController, decoration: InputDecoration(border: InputBorder.none), inputFormatters: [ LengthLimitingTextInputFormatter(10) //限制长度 ], ), ), IconButton( icon: Icon(Icons.search), onPressed: _openModalBottomSheet) ], ), ) ], )); } // void setExtAddress (String str)async{ // SharedPreferences prefs = await SharedPreferences.getInstance(); // prefs.setString("extAddress",str); // } }