map_view_page.dart 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. import 'dart:async';
  2. import 'dart:convert';
  3. import 'package:amap_location_example/map_util.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:amap_location/amap_export.dart';
  6. import 'dart:io';
  7. class MapViewPage extends StatefulWidget {
  8. @override
  9. State<StatefulWidget> createState() {
  10. return _MapViewPageState();
  11. }
  12. }
  13. class _MapViewPageState extends State<MapViewPage> with WidgetsBindingObserver {
  14. final Completer<AmapViewController> _controller = Completer<AmapViewController>();
  15. @override
  16. Widget build(BuildContext context) {
  17. return Scaffold(
  18. appBar: AppBar(title: Text("Map View")),
  19. body: SizedBox(
  20. height: double.maxFinite,
  21. width: double.maxFinite,
  22. child: AmapView(
  23. onPlatformViewCreated: _onPlatformCreate,
  24. ),
  25. ),
  26. );
  27. }
  28. @override
  29. void initState() {
  30. super.initState();
  31. WidgetsBinding.instance.addObserver(this);
  32. }
  33. @override
  34. void dispose() {
  35. WidgetsBinding.instance.removeObserver(this);
  36. _dispose();
  37. super.dispose();
  38. }
  39. @override
  40. void didChangeAppLifecycleState(AppLifecycleState state) {
  41. super.didChangeAppLifecycleState(state);
  42. onStateChange(state);
  43. }
  44. void onStateChange(AppLifecycleState state) async {
  45. if (!_controller.isCompleted)
  46. return;
  47. AmapViewController controller = await _controller.future;
  48. if (state == AppLifecycleState.paused) {
  49. controller.onPauseMapView();
  50. } else if (state == AppLifecycleState.resumed) {
  51. controller.onResumeMapView();
  52. }
  53. }
  54. void _initCityData(AmapViewController controller) async {
  55. // kingWay 的环境
  56. var httpClient = new HttpClient();
  57. HttpClientRequest request = await httpClient
  58. .getUrl(Uri.http("47.103.219.158:31103", "/api/v1/school_map/all_addr"));
  59. var response = await request.close();
  60. var responseBody = await response.transform(Utf8Decoder()).join();
  61. List<Map> data = jsonDecode(responseBody)['data'].cast<Map>();
  62. print(data.toString());
  63. controller.setMapMarkers(data.map((f) {
  64. var coordinate = f['coordinate'].toString().split(",");
  65. return AmapMarker(
  66. double.parse(coordinate[1]),
  67. double.parse(coordinate[0]),
  68. {"title": f["schoolname"], "content": f["address"], "tel": f["tel"]});
  69. }).toList());
  70. }
  71. void _onPlatformCreate(AmapViewController controller) {
  72. print("on map view create");
  73. _controller.complete(controller);
  74. controller.onCreateMapView();
  75. _initCityData(controller);
  76. controller.guideStream.listen((m) {
  77. showModalBottomSheet(
  78. context: context,
  79. builder: (context) {
  80. return Container(
  81. color: Colors.white,
  82. height: Platform.isIOS ? 158 + 50 : 158,
  83. margin: EdgeInsets.only(
  84. bottom: MediaQuery.of(context).padding.bottom,
  85. ),
  86. child: Column(
  87. mainAxisSize: MainAxisSize.max,
  88. mainAxisAlignment: MainAxisAlignment.center,
  89. crossAxisAlignment: CrossAxisAlignment.center,
  90. children: <Widget>[
  91. Offstage(
  92. offstage: !Platform.isIOS,
  93. child: Column(
  94. children: <Widget>[
  95. Container(
  96. height: 50,
  97. child: Material(
  98. color: Colors.white,
  99. child: InkWell(
  100. child: Center(
  101. child: Text(
  102. "苹果地图",
  103. style: TextStyle(
  104. fontSize: 15,
  105. ),
  106. ),
  107. ),
  108. onTap: () => _goThirdPartMapApp(context, MapType.Apple, m),
  109. ),
  110. ),
  111. ),
  112. Divider(
  113. height: 1,
  114. ),
  115. ],
  116. ),
  117. ),
  118. Container(
  119. height: 50,
  120. child: Material(
  121. color: Colors.white,
  122. child: InkWell(
  123. child: Center(
  124. child: Text(
  125. "高德地图",
  126. style: TextStyle(
  127. fontSize: 15,
  128. ),
  129. ),
  130. ),
  131. onTap: () => _goThirdPartMapApp(context, MapType.AMap, m),
  132. ),
  133. ),
  134. ),
  135. Divider(
  136. height: 1,
  137. ),
  138. Container(
  139. height: 50,
  140. child: Material(
  141. color: Colors.white,
  142. child: InkWell(
  143. child: Center(
  144. child: Text(
  145. "百度地图",
  146. style: TextStyle(
  147. fontSize: 15,
  148. ),
  149. ),
  150. ),
  151. onTap: () => _goThirdPartMapApp(context, MapType.Baidu, m),
  152. ),
  153. ),
  154. ),
  155. Divider(
  156. height: 1,
  157. ),
  158. Container(
  159. height: 50,
  160. child: Material(
  161. color: Colors.white,
  162. child: InkWell(
  163. child: Center(
  164. child: Text(
  165. "取消",
  166. style: TextStyle(
  167. fontSize: 15,
  168. ),
  169. ),
  170. ),
  171. onTap: () {
  172. Navigator.of(context).pop();
  173. },
  174. ),
  175. ),
  176. ),
  177. ],
  178. ),
  179. );
  180. });
  181. });
  182. }
  183. void _goThirdPartMapApp(BuildContext context, MapType type, dynamic data) async {
  184. // close bottom sheet
  185. Navigator.of(context).pop();
  186. bool success;
  187. if (type == MapType.AMap) {
  188. success = await MapUtil.goAMap(data['lon'], data['lat']);
  189. } else if (type == MapType.Baidu) {
  190. success = await MapUtil.goBaiduMap(data['lon'], data['lat']);
  191. } else if (type == MapType.Apple) {
  192. success = await MapUtil.goAppleMap(data['lon'], data['lat']);
  193. }
  194. print("jump ${success ? "successful" : "failed"}");
  195. }
  196. void _dispose() async {
  197. if (!_controller.isCompleted)
  198. return;
  199. AmapViewController controller = await _controller.future;
  200. controller.dispose();
  201. }
  202. }
  203. enum MapType {
  204. Apple, Baidu, AMap
  205. }