map_view_page.dart 6.4 KB

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