map_choicePoint.dart 13 KB

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