map_demo_page.dart 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. import 'dart:convert';
  2. import 'package:amap_flutter_base/amap_flutter_base.dart';
  3. import 'package:amap_flutter_map/amap_flutter_map.dart';
  4. import 'package:bbyyy/beans/poi_bean_entity.dart';
  5. import 'package:bbyyy/my_tools/map_util.dart';
  6. import 'package:bbyyy/my_tools/my_colors.dart';
  7. import 'package:bbyyy/my_tools/my_cookie.dart';
  8. import 'package:bbyyy/my_tools/my_tools.dart';
  9. import 'package:bbyyy/my_tools/my_views.dart';
  10. import 'package:dio/dio.dart';
  11. import 'package:flutter/cupertino.dart';
  12. import 'package:flutter/material.dart';
  13. class MapDemoPage extends StatefulWidget {
  14. @override
  15. _MapDemoPageState createState() => _MapDemoPageState();
  16. }
  17. class _MapDemoPageState extends State<MapDemoPage> {
  18. CameraPosition _kInitialPosition;
  19. List<Widget> _approvalNumberWidget = [];
  20. TextEditingController _controller = TextEditingController();
  21. Map<String, Marker> _markers = <String, Marker>{};
  22. Widget _poiInfo;
  23. @override
  24. void initState() {
  25. // TODO: implement initState
  26. super.initState();
  27. _kInitialPosition = CameraPosition(
  28. target:
  29. LatLng(MyCookie().location.latitude, MyCookie().location.longitude),
  30. zoom: 15.0,
  31. );
  32. }
  33. @override
  34. Widget build(BuildContext context) {
  35. final AMapWidget map = AMapWidget(
  36. initialCameraPosition: _kInitialPosition,
  37. onMapCreated: onMapCreated,
  38. rotateGesturesEnabled: false,
  39. touchPoiEnabled: true,
  40. onPoiTouched: _onPoiTouched,
  41. myLocationStyleOptions: MyLocationStyleOptions(
  42. true,
  43. ),
  44. markers: Set<Marker>.of(_markers.values),
  45. );
  46. return Scaffold(
  47. body: Column(
  48. children: [
  49. MyViews().myAppBar('amap', context, []),
  50. Expanded(
  51. child: Stack(
  52. children: [
  53. Container(
  54. height: MediaQuery.of(context).size.height,
  55. width: MediaQuery.of(context).size.width,
  56. child: map,
  57. ),
  58. Container(
  59. child: Row(
  60. children: [
  61. Expanded(
  62. child: Padding(
  63. padding: EdgeInsets.symmetric(horizontal: 10),
  64. child: TextField(
  65. controller: _controller,
  66. cursorColor: MyColors.cFF4233,
  67. cursorWidth: 1.0,
  68. decoration: InputDecoration(
  69. border: InputBorder.none,
  70. disabledBorder: InputBorder.none,
  71. enabledBorder: InputBorder.none,
  72. focusedBorder: InputBorder.none,
  73. hintText: '请输入地址',
  74. hintStyle: TextStyle(
  75. color: MyColors.c999999,
  76. fontSize: 16),
  77. isDense: true,
  78. contentPadding: const EdgeInsets.fromLTRB(
  79. 14, 4.5, 8, 4.5)),
  80. maxLines: 1,
  81. style: TextStyle(
  82. color: MyColors.c333333,
  83. fontSize: 16,
  84. height: 1.3,
  85. letterSpacing: 0.2),
  86. onChanged: (t) {},
  87. ),
  88. ),
  89. ),
  90. GestureDetector(
  91. onTap: () {
  92. inquirePOI();
  93. },
  94. child: Container(
  95. color: Colors.blue,
  96. child: Text(
  97. '搜索',
  98. style: TextStyle(color: Colors.white, fontSize: 18),
  99. ),
  100. width: 100,
  101. height: 48,
  102. alignment: Alignment.center,
  103. ),
  104. ),
  105. ],
  106. ),
  107. color: Colors.white,
  108. margin: EdgeInsets.symmetric(horizontal: 15,vertical: 30),
  109. )
  110. ],
  111. ),
  112. ),
  113. ],
  114. ),
  115. resizeToAvoidBottomInset: false,
  116. );
  117. }
  118. AMapController _mapController;
  119. void onMapCreated(AMapController controller) {
  120. setState(() {
  121. _mapController = controller;
  122. getApprovalNumber();
  123. });
  124. }
  125. /// 获取审图号
  126. void getApprovalNumber() async {
  127. //普通地图审图号
  128. String mapContentApprovalNumber =
  129. await _mapController?.getMapContentApprovalNumber();
  130. //卫星地图审图号
  131. String satelliteImageApprovalNumber =
  132. await _mapController?.getSatelliteImageApprovalNumber();
  133. setState(() {
  134. if (null != mapContentApprovalNumber) {
  135. _approvalNumberWidget.add(Text(mapContentApprovalNumber));
  136. }
  137. if (null != satelliteImageApprovalNumber) {
  138. _approvalNumberWidget.add(Text(satelliteImageApprovalNumber));
  139. }
  140. });
  141. print('地图审图号(普通地图): $mapContentApprovalNumber');
  142. print('地图审图号(卫星地图): $satelliteImageApprovalNumber');
  143. }
  144. Widget showPoiInfo(AMapPoi poi) {
  145. return Container(
  146. alignment: Alignment.center,
  147. color: Color(0x8200CCFF),
  148. child: Text(
  149. '您点击了 ${poi.name}',
  150. style: TextStyle(fontWeight: FontWeight.w600),
  151. ),
  152. );
  153. }
  154. void _onPoiTouched(AMapPoi poi) {
  155. setState(() {
  156. _poiInfo = showPoiInfo(poi);
  157. });
  158. }
  159. void inquirePOI() {
  160. Dio()
  161. .get(
  162. 'https://restapi.amap.com/v3/place/text?keywords=${_controller.text.toString()}&city=${MyCookie().location.city}&output=json&offset=25&page=1&key=5dcd9f0ed7d51aefb5b2f73dba1069cb&extensions=all')
  163. .then((value) {
  164. print(
  165. 'https://restapi.amap.com/v3/place/text?keywords=${_controller.text.toString()}&city=${MyCookie().location.city}&output=json&offset=25&page=1&key=5dcd9f0ed7d51aefb5b2f73dba1069cb&extensions=all');
  166. print(value);
  167. PoiBeanEntity entity =
  168. PoiBeanEntity().fromJson(json.decode(value.toString()));
  169. entity.pois.forEach((element) {
  170. print(element.name);
  171. _markers[element.id] = Marker(
  172. position: LatLng(double.parse(element.location.split(',')[1]),
  173. double.parse(element.location.split(',')[0])),
  174. icon: BitmapDescriptor.fromIconPath('images/地址.png'),
  175. infoWindow: InfoWindow(title: element.name),
  176. // onTap: (markerId) {
  177. // print(markerId);
  178. // MapUtil.gotoAMap(double.parse(element.location.split(',')[0]),
  179. // double.parse(element.location.split(',')[1]));
  180. // },
  181. // onDragEnd: (markerId, endPosition) =>
  182. // _onMarkerDragEnd(markerId, endPosition),
  183. );
  184. });
  185. setState(() {
  186. if (entity.pois.length != 0) {
  187. _mapController
  188. .moveCamera(CameraUpdate.newCameraPosition(CameraPosition(
  189. target: LatLng(double.parse(entity.pois[0].location.split(',')[1]),
  190. double.parse(entity.pois[0].location.split(',')[0])),
  191. zoom: 18,
  192. )));
  193. }else{
  194. showToast('未搜索到你查找的地点');
  195. }
  196. });
  197. });
  198. }
  199. }