map_demo_page.dart 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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/my_cookie.dart';
  6. import 'package:dio/dio.dart';
  7. import 'package:flutter/material.dart';
  8. class MapDemoPage extends StatefulWidget {
  9. @override
  10. _MapDemoPageState createState() => _MapDemoPageState();
  11. }
  12. class _MapDemoPageState extends State<MapDemoPage> {
  13. CameraPosition _kInitialPosition;
  14. List<Widget> _approvalNumberWidget = [];
  15. TextEditingController _controller = TextEditingController();
  16. Map<String, Marker> _markers = <String, Marker>{};
  17. Widget _poiInfo;
  18. @override
  19. void initState() {
  20. // TODO: implement initState
  21. super.initState();
  22. _kInitialPosition = CameraPosition(
  23. target:
  24. LatLng(MyCookie().location.latitude, MyCookie().location.longitude),
  25. zoom: 15.0,
  26. );
  27. }
  28. @override
  29. Widget build(BuildContext context) {
  30. final AMapWidget map = AMapWidget(
  31. initialCameraPosition: _kInitialPosition,
  32. onMapCreated: onMapCreated,
  33. touchPoiEnabled: true,
  34. onPoiTouched: _onPoiTouched,
  35. myLocationStyleOptions: MyLocationStyleOptions(
  36. true,
  37. circleFillColor: Colors.lightBlue,
  38. circleStrokeColor: Colors.blue,
  39. circleStrokeWidth: 1,
  40. ),
  41. );
  42. return Scaffold(
  43. body: Stack(
  44. children: [
  45. Container(
  46. height: MediaQuery.of(context).size.height,
  47. width: MediaQuery.of(context).size.width,
  48. child: map,
  49. ),
  50. Container(
  51. child: Row(
  52. children: [
  53. Expanded(
  54. child: TextField(
  55. controller: _controller,
  56. )),
  57. GestureDetector(
  58. onTap: () {
  59. inquirePOI();
  60. },
  61. child: Container(
  62. color: Colors.blue,
  63. child: Text(
  64. '搜索',
  65. style: TextStyle(color: Colors.white, fontSize: 18),
  66. ),
  67. width: 100,
  68. height: 48,
  69. alignment: Alignment.center,
  70. ))
  71. ],
  72. ),
  73. color: Colors.white,
  74. margin: EdgeInsets.only(top: 50),
  75. )
  76. ],
  77. ),
  78. );
  79. }
  80. AMapController _mapController;
  81. void onMapCreated(AMapController controller) {
  82. setState(() {
  83. _mapController = controller;
  84. getApprovalNumber();
  85. });
  86. }
  87. /// 获取审图号
  88. void getApprovalNumber() async {
  89. //普通地图审图号
  90. String mapContentApprovalNumber =
  91. await _mapController?.getMapContentApprovalNumber();
  92. //卫星地图审图号
  93. String satelliteImageApprovalNumber =
  94. await _mapController?.getSatelliteImageApprovalNumber();
  95. setState(() {
  96. if (null != mapContentApprovalNumber) {
  97. _approvalNumberWidget.add(Text(mapContentApprovalNumber));
  98. }
  99. if (null != satelliteImageApprovalNumber) {
  100. _approvalNumberWidget.add(Text(satelliteImageApprovalNumber));
  101. }
  102. });
  103. print('地图审图号(普通地图): $mapContentApprovalNumber');
  104. print('地图审图号(卫星地图): $satelliteImageApprovalNumber');
  105. }
  106. Widget showPoiInfo(AMapPoi poi) {
  107. return Container(
  108. alignment: Alignment.center,
  109. color: Color(0x8200CCFF),
  110. child: Text(
  111. '您点击了 ${poi.name}',
  112. style: TextStyle(fontWeight: FontWeight.w600),
  113. ),
  114. );
  115. }
  116. void _onPoiTouched(AMapPoi poi) {
  117. setState(() {
  118. _poiInfo = showPoiInfo(poi);
  119. });
  120. }
  121. void inquirePOI() {
  122. Dio()
  123. .get(
  124. 'https://restapi.amap.com/v3/place/text?keywords=${_controller.text.toString()}&city=${MyCookie().location.city}&output=json&offset=50&page=1&key=5dcd9f0ed7d51aefb5b2f73dba1069cb&extensions=all')
  125. .then((value) {
  126. print('https://restapi.amap.com/v3/place/text?keywords=${_controller.text.toString()}&city=${MyCookie().location.city}&output=json&offset=50&page=1&key=5dcd9f0ed7d51aefb5b2f73dba1069cb&extensions=all');
  127. print(value);
  128. PoiBeanEntity entity = PoiBeanEntity().fromJson(json.decode(value.toString()));
  129. entity.pois.forEach((element) {
  130. print(element.name);
  131. _markers[element.name] = Marker(
  132. position: LatLng(double.parse(element.location.split(',')[0]),double.parse(element.location.split(',')[1])),
  133. icon: BitmapDescriptor.fromIconPath('images/地址.png'),
  134. infoWindow: InfoWindow(title: element.name),
  135. // onTap: (markerId) => _onMarkerTapped(markerId),
  136. // onDragEnd: (markerId, endPosition) =>
  137. // _onMarkerDragEnd(markerId, endPosition),
  138. );
  139. });
  140. });
  141. }
  142. }