map_view_page.dart 6.4 KB

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