map_demo_page.dart 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. tiltGesturesEnabled: false,
  41. onPoiTouched: _onPoiTouched,
  42. myLocationStyleOptions: MyLocationStyleOptions(
  43. true,
  44. ),
  45. markers: Set<Marker>.of(_markers.values),
  46. );
  47. return Scaffold(
  48. body: Column(
  49. children: [
  50. MyViews().myAppBar('amap', context, []),
  51. Expanded(
  52. child: Stack(
  53. children: [
  54. Container(
  55. height: MediaQuery.of(context).size.height,
  56. width: MediaQuery.of(context).size.width,
  57. child: map,
  58. ),
  59. Container(
  60. child: Row(
  61. children: [
  62. Expanded(
  63. child: Padding(
  64. padding: EdgeInsets.symmetric(horizontal: 10),
  65. child: TextField(
  66. controller: _controller,
  67. cursorColor: MyColors.cFF4233,
  68. cursorWidth: 1.0,
  69. decoration: InputDecoration(
  70. border: InputBorder.none,
  71. disabledBorder: InputBorder.none,
  72. enabledBorder: InputBorder.none,
  73. focusedBorder: InputBorder.none,
  74. hintText: '请输入地址',
  75. hintStyle: TextStyle(
  76. color: MyColors.c999999, fontSize: 16),
  77. isDense: true,
  78. contentPadding:
  79. const EdgeInsets.fromLTRB(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. String v1 =
  161. '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';
  162. print(v1);
  163. Dio().get(v1).then((value) {
  164. print(value);
  165. PoiBeanEntity entity =
  166. PoiBeanEntity().fromJson(json.decode(value.toString()));
  167. entity.pois.forEach((element) {
  168. print(element.name);
  169. _markers[element.id] = Marker(
  170. position: LatLng(double.parse(element.location.split(',')[1]),
  171. double.parse(element.location.split(',')[0])),
  172. icon: BitmapDescriptor.fromIconPath('images/地址.png'),
  173. infoWindow: InfoWindow(title: element.name),
  174. onTap: (markerId) {
  175. print(markerId);
  176. MapUtil.gotoAMap(double.parse(element.location.split(',')[0]),
  177. double.parse(element.location.split(',')[1]));
  178. },
  179. );
  180. });
  181. setState(() {
  182. if (entity.pois.length != 0) {
  183. _mapController
  184. .moveCamera(CameraUpdate.newCameraPosition(CameraPosition(
  185. target: LatLng(double.parse(entity.pois[0].location.split(',')[1]),
  186. double.parse(entity.pois[0].location.split(',')[0])),
  187. zoom: 18,
  188. )));
  189. } else {
  190. showToast('未搜索到你查找的地点');
  191. }
  192. });
  193. });
  194. }
  195. }