map_view_page.dart 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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.getUrl(
  58. 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: 208,
  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: () =>
  109. _goThirdPartMapApp(context, MapType.Apple, m),
  110. ),
  111. ),
  112. ),
  113. Divider(
  114. height: 1,
  115. ),
  116. ],
  117. ),
  118. ),
  119. Container(
  120. height: 50,
  121. child: Material(
  122. color: Colors.white,
  123. child: InkWell(
  124. child: Center(
  125. child: Text(
  126. "高德地图",
  127. style: TextStyle(
  128. fontSize: 15,
  129. ),
  130. ),
  131. ),
  132. onTap: () =>
  133. _goThirdPartMapApp(context, MapType.AMap, m),
  134. ),
  135. ),
  136. ),
  137. Divider(
  138. height: 1,
  139. ),
  140. Container(
  141. height: 50,
  142. child: Material(
  143. color: Colors.white,
  144. child: InkWell(
  145. child: Center(
  146. child: Text(
  147. "百度地图",
  148. style: TextStyle(
  149. fontSize: 15,
  150. ),
  151. ),
  152. ),
  153. onTap: () =>
  154. _goThirdPartMapApp(context, MapType.Baidu, m),
  155. ),
  156. ),
  157. ),
  158. Divider(
  159. height: 1,
  160. ),
  161. Container(
  162. height: 50,
  163. child: Material(
  164. color: Colors.white,
  165. child: InkWell(
  166. child: Center(
  167. child: Text(
  168. "取消",
  169. style: TextStyle(
  170. fontSize: 15,
  171. ),
  172. ),
  173. ),
  174. onTap: () {
  175. Navigator.of(context).pop();
  176. },
  177. ),
  178. ),
  179. ),
  180. ],
  181. ),
  182. );
  183. });
  184. });
  185. }
  186. void _goThirdPartMapApp(
  187. BuildContext context, MapType type, dynamic data) async {
  188. // close bottom sheet
  189. Navigator.of(context).pop();
  190. bool success;
  191. if (type == MapType.AMap) {
  192. success = await MapUtil.goAMap(data['lon'], data['lat']);
  193. } else if (type == MapType.Baidu) {
  194. success = await MapUtil.goBaiduMap(data['lon'], data['lat']);
  195. } else if (type == MapType.Apple) {
  196. AmapLocation.instance.appleNavigate(data['lat'], data['lon'],data['title']);
  197. }
  198. print("jump ${success ? "successful" : "failed"}");
  199. }
  200. void _dispose() async {
  201. if (!_controller.isCompleted)
  202. return;
  203. AmapViewController controller = await _controller.future;
  204. controller.dispose();
  205. }
  206. }
  207. enum MapType { Apple, Baidu, AMap }