map_choicePoint.dart 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. import 'package:amap_map_fluttify/amap_map_fluttify.dart';
  2. import 'package:amap_search_fluttify/amap_search_fluttify.dart';
  3. import 'package:flutter/material.dart';
  4. // import 'package:decorated_flutter/decorated_flutter.dart';
  5. import 'package:flutter/services.dart';
  6. import 'package:permission_handler/permission_handler.dart';
  7. import 'package:liftmanager/widgets/app_bar.dart';
  8. import 'package:liftmanager/utils/toast.dart';
  9. import 'package:liftmanager/routers/fluro_navigator.dart';
  10. import 'package:shared_preferences/shared_preferences.dart';
  11. import 'package:liftmanager/utils/fast_notification.dart';
  12. import '../../../utils/toast.dart';
  13. import '../../../utils/toast.dart';
  14. import '../../../utils/toast.dart';
  15. /**
  16. * 地图选择点控件
  17. */
  18. class MyHomePage extends StatefulWidget {
  19. /**
  20. * 选择点后回调事件
  21. */
  22. // final Function onChoicePoint;
  23. // MyHomePage(this.onChoicePoint);
  24. MyHomePage(this.type);
  25. final String type;
  26. @override
  27. _MapChoicePointState createState() => _MapChoicePointState();
  28. }
  29. class _MapChoicePointState extends State<MyHomePage>
  30. with SingleTickerProviderStateMixin {
  31. //----属性----
  32. //地图控制器
  33. AmapController _amapController;
  34. //选择的点
  35. Marker _markerSelect;
  36. //搜索出来之后选择的点
  37. Marker _markerSeached;
  38. //所在城市
  39. String city;
  40. //搜索框文字控制器
  41. TextEditingController _serachController;
  42. //自定义marker点图标图片路径
  43. Uri _imgUri = Uri.parse('images/icon_star_selected.png');
  44. //----方法----
  45. /**
  46. * 获取权限
  47. */
  48. Future<bool> _requestPermission() async {
  49. final permissions = await PermissionHandler()
  50. .requestPermissions([PermissionGroup.location]);
  51. if (permissions[PermissionGroup.location] == PermissionStatus.granted) {
  52. return true;
  53. } else {
  54. // toasts('需要定位权限!');
  55. return false;
  56. }
  57. }
  58. /**
  59. * 根据搜索条件选出想要的点
  60. */
  61. Future _openModalBottomSheet() async {
  62. //收起键盘
  63. print("----------");
  64. FocusScope.of(context).requestFocus(FocusNode());
  65. //根据关键字及城市进行搜索
  66. final poiList = await AmapSearch.searchKeyword(
  67. _serachController.text,
  68. city: city,
  69. );
  70. List<Map> points = [];
  71. //便利拼接信息
  72. for (var item in poiList) {
  73. points.add({
  74. 'title': await item.title,
  75. 'address': await item.adName + await item.address,
  76. 'position': await item.latLng,
  77. });
  78. }
  79. //弹出底部对话框并等待选择
  80. final option = await showModalBottomSheet(
  81. context: context,
  82. builder: (BuildContext context) {
  83. return points.length > 0
  84. ? ListView.builder(
  85. itemCount: points.length,
  86. itemBuilder: (BuildContext itemContext, int i) {
  87. return ListTile(
  88. title: Text(points[i]['title']),
  89. subtitle: Text(points[i]['address']),
  90. onTap: () {
  91. Navigator.pop(context, points[i]);
  92. print(points[i]);
  93. print(4444444);
  94. toCenter(points[i]);
  95. },
  96. );
  97. },
  98. )
  99. : Container(
  100. alignment: Alignment.center,
  101. padding: EdgeInsets.all(40),
  102. child: Text('暂无数据'),
  103. );
  104. },
  105. );
  106. if (option != null) {
  107. LatLng selectlatlng = option['position'];
  108. //将地图中心点移动到选择的点
  109. _amapController.setCenterCoordinate(
  110. LatLng(selectlatlng.latitude, selectlatlng.longitude));
  111. //删除原来地图上搜索出来的点
  112. if (_markerSeached != null) {
  113. _markerSeached.remove();
  114. }
  115. //将搜索出来的点显示在界面上 --此处不能使用自定义图标的marker,使用会报错,至今也没有解决
  116. _markerSeached = await _amapController.addMarker(MarkerOption(
  117. latLng: selectlatlng,
  118. ));
  119. }
  120. }
  121. Future toCenter(option) async {
  122. print(option['position']);
  123. print("======================");
  124. LatLng selectlatlng = option['position'];
  125. //将地图中心点移动到选择的点
  126. _amapController.setCenterCoordinate(
  127. LatLng(selectlatlng.latitude, selectlatlng.longitude));
  128. //删除原来地图上搜索出来的点
  129. if (_markerSeached != null) {
  130. _markerSeached.remove();
  131. }
  132. //将搜索出来的点显示在界面上 --此处不能使用自定义图标的marker,使用会报错,至今也没有解决
  133. _markerSeached = await _amapController.addMarker(MarkerOption(
  134. latLng: selectlatlng,
  135. ));
  136. }
  137. @override
  138. void initState() {
  139. super.initState();
  140. _serachController = TextEditingController();
  141. _requestPermission();
  142. }
  143. @override
  144. void dispose() {
  145. super.dispose();
  146. }
  147. @override
  148. Widget build(BuildContext context) {
  149. return Scaffold(
  150. appBar: MyAppBar(
  151. centerTitle: "地图选择",
  152. ),
  153. body: Stack(
  154. alignment: AlignmentDirectional.topCenter,
  155. children: <Widget>[
  156. AmapView(
  157. // 地图类型 (可选)
  158. mapType: MapType.Standard,
  159. // 是否显示缩放控件 (可选)
  160. // showZoomControl: true,
  161. // 是否显示指南针控件 (可选)
  162. showCompass: true,
  163. // 是否显示比例尺控件 (可选)
  164. showScaleControl: true,
  165. // 是否使能缩放手势 (可选)
  166. zoomGesturesEnabled: true,
  167. // 是否使能滚动手势 (可选)
  168. scrollGesturesEnabled: true,
  169. // 是否使能旋转手势 (可选)
  170. rotateGestureEnabled: false,
  171. // 是否使能倾斜手势 (可选)
  172. tiltGestureEnabled: false,
  173. // 缩放级别 (可选)
  174. zoomLevel: 16,
  175. // 中心点坐标 (可选)
  176. // centerCoordinate: LatLng(39, 116),
  177. // 标记 (可选)
  178. markers: <MarkerOption>[],
  179. // 标识点击回调 (可选)
  180. onMarkerClicked: (Marker marker) async {
  181. if (_markerSeached == null) {
  182. return;
  183. }
  184. //获取点击点的位置
  185. var location = await marker.location;
  186. var lon = location.longitude;
  187. var lat = location.latitude;
  188. //获取搜索点的位置
  189. var slocation = await _markerSeached.location;
  190. var slon = slocation.longitude;
  191. var slat = slocation.latitude;
  192. //比较位置
  193. if (lon == slon && lat == slat) {
  194. //移除原来的点
  195. if (_markerSeached != null) {
  196. _markerSeached.remove();
  197. }
  198. if (_markerSelect != null) {
  199. _markerSelect.remove();
  200. }
  201. //画上新的点
  202. _markerSelect = await _amapController.addMarker(
  203. MarkerOption(
  204. latLng: location,
  205. // iconUri: _imgUri,
  206. // imageConfig: createLocalImageConfiguration(context),
  207. width: 64,
  208. height: 64,
  209. anchorV: 0.7,
  210. anchorU: 0.5,
  211. ),
  212. );
  213. }
  214. },
  215. // 地图点击回调 (可选)
  216. onMapClicked: (LatLng coord) async {
  217. print("//////////////");
  218. print(coord.latitude);
  219. print(coord.longitude);
  220. String address;
  221. String addressM;
  222. /// 逆地理编码(坐标转地址)
  223. ReGeocode reGeocodeList = await AmapSearch.searchReGeocode(
  224. coord,
  225. );
  226. print(await reGeocodeList.toFutureString());
  227. print(await reGeocodeList.provinceName);
  228. print(await reGeocodeList.cityName);
  229. print(await reGeocodeList.districtName);
  230. print("reGeocodeList.provinceName=================");
  231. addressM = (await reGeocodeList.provinceName) + (await reGeocodeList.cityName) + (await reGeocodeList.districtName);
  232. address = await reGeocodeList.formatAddress;
  233. print(address);
  234. print(address +
  235. "," +
  236. coord.latitude.toString() +
  237. "," +
  238. coord.longitude.toString());
  239. if (_amapController != null) {
  240. //移除原来的点
  241. if (_markerSelect != null) {
  242. _markerSelect.remove();
  243. }
  244. if (_markerSeached != null) {
  245. _markerSeached.remove();
  246. }
  247. //画上新的点
  248. _markerSelect = await _amapController.addMarker(MarkerOption(
  249. latLng: coord,
  250. // iconUri: _imgUri,
  251. // imageConfig: createLocalImageConfiguration(context),
  252. width: 64,
  253. height: 64,
  254. anchorV: 0.7,
  255. anchorU: 0.5));
  256. // widget.onChoicePoint(coord);
  257. showAlert(
  258. context,
  259. "提示",
  260. "出诊地址为: $address",
  261. "确定",
  262. () {
  263. Navigator.of(context)..pop()..pop();
  264. // setExtAddress(address+","+coord.latitude.toString()+","+coord.longitude.toString());
  265. String setAddress = address +
  266. "," +
  267. coord.latitude.toString() +
  268. "," +
  269. coord.longitude.toString();
  270. String setAddressMaster = addressM;
  271. if(widget.type == "1"){
  272. FastNotification.push("set_address", setAddress);
  273. }else if (widget.type == "2"){
  274. FastNotification.push("set_address_master", setAddressMaster);
  275. }
  276. },
  277. txt2: "取消",
  278. onPre2: () {
  279. NavigatorUtils.goBack(context);
  280. },
  281. );
  282. }
  283. },
  284. onMapMoveStart: (MapMove move) {},
  285. // 地图创建完成回调 (可选)
  286. onMapCreated: (controller) async {
  287. _amapController = controller;
  288. //申请权限
  289. if (await _requestPermission()) {
  290. //获取所在城市
  291. print(123456789);
  292. // final location = await AmapLocation.fetchLocation();
  293. // city = await location.city;
  294. // print(location);
  295. // print(city);
  296. print(1111111);
  297. //显示自己的定位
  298. await controller.showMyLocation(MyLocationOption());
  299. // await initSerach();
  300. }
  301. },
  302. ),
  303. Container(
  304. margin: EdgeInsets.all(20),
  305. width: MediaQuery.of(context).size.width,
  306. height: 46,
  307. decoration: BoxDecoration(color: Colors.white),
  308. child: Row(
  309. mainAxisAlignment: MainAxisAlignment.spaceAround,
  310. children: <Widget>[
  311. Container(
  312. padding: EdgeInsets.all(8),
  313. width: MediaQuery.of(context).size.width - 20 - 80,
  314. child: TextField(
  315. controller: _serachController,
  316. decoration: InputDecoration(border: InputBorder.none),
  317. inputFormatters: <TextInputFormatter>[
  318. LengthLimitingTextInputFormatter(10) //限制长度
  319. ],
  320. ),
  321. ),
  322. IconButton(
  323. icon: Icon(Icons.search),
  324. onPressed: _openModalBottomSheet)
  325. ],
  326. ),
  327. )
  328. ],
  329. ));
  330. }
  331. // void setExtAddress (String str)async{
  332. // SharedPreferences prefs = await SharedPreferences.getInstance();
  333. // prefs.setString("extAddress",str);
  334. // }
  335. }