index.vue 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <template>
  2. <div >
  3. <!-- <el-row>
  4. <Button @click="drawFence">添加区域</Button>
  5. <Button @click="drawFence">清除区域</Button>
  6. <Button @click="saveRectangle">保存区域</Button>
  7. </el-row> -->
  8. <el-row style="height: 1300px;width: 100%;">
  9. <el-amap class="amap-box" vid="map"
  10. :zoom="zoom"
  11. :center="center"
  12. :amap-manager="amapManager"
  13. :events="events"
  14. >
  15. <el-amap-marker v-for="(u,i) in markers" :position="u.position" :key="i" :icon="icon">
  16. </el-amap-marker>
  17. </el-amap>
  18. </el-row>
  19. <el-row type="flex" justify="end" class="m_tools">
  20. <el-col style="width:auto;">
  21. <el-button-group size="small">
  22. <el-button type="default" title="绘制围栏" size="small" v-if="tools_show.place_show" v-show="!tools_show.oc_show" icon="el-icon-edit" @click="drawFence"></el-button>
  23. <el-button type="default" title="保存围栏" size="small" v-if="tools_show.place_show" v-show="!tools_show.oc_show" icon="el-icon-check" @click="saveRectangle" ></el-button>
  24. <el-button type="default" title="清除" size="small"v-if="tools_show.place_show" v-show="!tools_show.oc_show" icon="el-icon-delete" @click="delemap"></el-button>
  25. <el-button type="default" title="展开/收起" size="small" v-if="tools_show.openclose_show" :icon="tools_show.oc_show?'el-icon-d-arrow-left':'el-icon-d-arrow-right'" @click="tools_show.oc_show=!tools_show.oc_show"></el-button>
  26. </el-button-group>
  27. </el-col>
  28. </el-row>
  29. </div>
  30. </template>
  31. <script>
  32. import VueAMap from 'vue-amap'
  33. let amapManager = new VueAMap.AMapManager()
  34. window._AMapSecurityConfig = {
  35. securityJsCode: 'b672db78212bb58899d609f0ce9f2d97'
  36. }
  37. export default {
  38. name: "Map",
  39. data() {
  40. return {
  41. // 地图对象
  42. amapManager,
  43. zoom: 12,
  44. center: [116.404269,39.916042],
  45. position: [121.5273285, 31.21515044],
  46. events: {
  47. init (o) {
  48. }
  49. },
  50. icon: new AMap.Icon({
  51. image: "https://webapi.amap.com/theme/v1.3/markers/n/mark_b.png"
  52. }),
  53. searchOption: {
  54. city: '北京',
  55. citylimit: false
  56. },
  57. fenceForm:{
  58. coordinate:[]
  59. },
  60. rectangle:null,
  61. mouseTool: null,
  62. overlays: [],
  63. map: null,
  64. markers: [//标记点位置
  65. ],
  66. formData: {
  67. carId: '',
  68. pageNum: 1,//当前页
  69. pageSize: 10,//页长
  70. pageTotal: 0,//总数
  71. },
  72. map: null,
  73. path: [],//以前绘制的数据
  74. paths: [], // 当前绘制的多边形经纬度数组
  75. polygonItems: [], // 地图上绘制的所有多边形对象
  76. polyEditors: [],// 新增数据=>所有编辑对象数组
  77. polyEditorsBefore: [],// 以前历史数据=>进入编辑对象数组
  78. // 总条数
  79. tools_show: {
  80. place_show:true,
  81. openclose_show:true,
  82. oc_show:false
  83. },
  84. // 查询参数
  85. queryParams: {
  86. pageNum: 1,
  87. pageSize: 10,
  88. fenceName: null,
  89. deptId: null,
  90. },
  91. // 表单参数
  92. form: {}
  93. };
  94. },
  95. watch: {
  96. },
  97. mounted() {
  98. //DOM初始化完成进行地图初始化
  99. //this.initAMap()
  100. },
  101. methods: {
  102. // 绘制多边形
  103. drawFence() {
  104. let _this = this
  105. this.map = this.amapManager.getMap()
  106. let map = this.amapManager.getMap()
  107. /* if(this.fenceForm.coordinate.length >0){
  108. this.$Message.warning("围栏已存在!")
  109. return
  110. }*/
  111. if(this.rectangle){
  112. map.remove(this.rectangle)
  113. }
  114. map.plugin(['AMap.MouseTool'], function () {
  115. let mouseTool = new AMap.MouseTool(map)
  116. _this.mouseTool = mouseTool
  117. //添加事件
  118. /*mouseTool.rectangle()
  119. AMap.event.addListener(mouseTool, 'draw', function (e) {
  120. _this.fenceForm.coordinate = []
  121. let path = e.obj.getPath()
  122. path.forEach(e=>{
  123. _this.fenceForm.coordinate.push([e.getLng(),e.getLat()])
  124. })
  125. mouseTool.close(false)
  126. })*/
  127. const polygon = mouseTool.polygon({
  128. //polygon:绘制多边形【线段:polyline;矩形:rectangle;圆:circle】
  129. strokeColor: 'red',
  130. strokeOpacity: 0.4,
  131. strokeWeight: 2,//线宽
  132. fillColor: '#1791fc', //填充色
  133. fillOpacity: 0.2,//填充透明度
  134. // strokeStyle还支持 solid
  135. strokeStyle: 'solid',
  136. // strokeDasharray: [30,10],
  137. });
  138. mouseTool.on('draw', function (event) {
  139. // event.obj 为绘制出来的覆盖物对象
  140. let polygonItem = event.obj;
  141. let paths = polygonItem.getPath();//取得绘制的多边形的每一个点坐标
  142. // console.log('覆盖物对象绘制完成各个点的坐标', paths, event);
  143. let path_in = []; // 编辑的路径
  144. paths.forEach(v => {
  145. path_in.push([v.lng, v.lat])
  146. _this.fenceForm.coordinate.push([v.lng, v.lat])
  147. });
  148. _this.paths = path_in //将新增数据放入paths数组里
  149. _this.path = path_in //将新增数据放入paths数组里
  150. //this.editRectangle();//绘制完成,默认进入编辑状态
  151. var polygonItems=[];
  152. polygonItems .push(event.obj);
  153. //this.map.remove(event.obj); // 删除多边形
  154. console.log(_this.paths, '------polygon-----');
  155. });
  156. })
  157. },
  158. // 编辑围栏
  159. editRectangle() {
  160. const path = this.paths;
  161. //新增的进入编辑状态
  162. let polygon = new AMap.Polygon({
  163. path: path,
  164. strokeColor: "#FF33FF",
  165. strokeWeight: 6,
  166. strokeOpacity: 0.2,
  167. fillOpacity: 0.2,
  168. fillColor: '#1791fc',
  169. zIndex: 50,
  170. });
  171. this.map.add(polygon);
  172. this.polygonItem.push(polygon);
  173. // 缩放地图到合适的视野级别
  174. this.map.setFitView([polygon]);
  175. this.polyEditor = new AMap.PolyEditor(this.map, polygon);
  176. this.polyEditor.open();
  177. this.polyEditors.push(this.polyEditor);
  178. //历史围栏的进入编辑状态
  179. let polygonBefore = new AMap.Polygon({
  180. path: this.path,
  181. strokeColor: "#FF33FF",
  182. strokeWeight: 6,
  183. strokeOpacity: 0.2,
  184. fillOpacity: 0.2,
  185. fillColor: '#1791fc',
  186. zIndex: 50,
  187. });
  188. this.map.add(polygonBefore);
  189. this.polygonItem.push(polygonBefore);
  190. // 缩放地图到合适的视野级别
  191. this.map.setFitView([polygonBefore]);
  192. this.polyEditorBefore = new AMap.PolyEditor(this.map, polygonBefore);
  193. this.polyEditorBefore.open();
  194. this.polyEditorsBefore.push(this.polyEditorBefore);
  195. // this.polyEditor.on('addnode', function (event) {
  196. // console.info('触发事件:addnode', event)
  197. // console.info('修改后的经纬度:', polygon.getPath())
  198. // });
  199. // this.polyEditor.on('adjust', function (event) {
  200. // console.info('触发事件:adjust', event)
  201. // console.info('修改后的经纬度:', polygon.getPath())
  202. // });
  203. // this.polyEditor.on('removenode', function (event) {
  204. // console.info('触发事件:removenode', event)
  205. // console.info('修改后的经纬度:', polygon.getPath())
  206. // });
  207. // this.polyEditor.on('end', function (event) {
  208. // console.info('触发事件: end', event)
  209. // console.info('end修改后的经纬度:', polygon.getPath())
  210. // // event.target 即为编辑后的多边形对象
  211. // });
  212. },
  213. // 取消编辑状态
  214. cancelRectangle() {
  215. this.polyEditors.forEach(item => { item.close(); });//新增
  216. this.polyEditorsBefore.forEach(item => { item.close(); });//历史
  217. },
  218. //保存围栏
  219. saveRectangle() {
  220. console.log(this.path,'---save----');
  221. //保存更新围栏经纬度
  222. let polygon="";
  223. var polygon_start="POLYGON((";
  224. this.path.forEach(v => {
  225. polygon=polygon+v[0]+" "+v[1]+",";
  226. });
  227. let result = polygon_start+polygon.slice(0, polygon.length - 1)+","+polygon.split(",")[0]+"))";
  228. let id = this.$route.params.fenceId
  229. let fencePolygon={fenceId:id,fencePolygon:result};
  230. },
  231. // 删除围栏
  232. deleRectangle() {
  233. this.map.clearMap(); // 删除地图所有覆盖物
  234. //删除=>成功(重新刷新页面)
  235. },
  236. delemap() {
  237. this.map.clearMap(); // 删除地图所有覆盖物
  238. },
  239. }
  240. };
  241. </script>
  242. <style lang="scss" scoped>
  243. ::v-deep .amap-logo {
  244. display: none;
  245. opacity: 0 !important;
  246. }
  247. ::v-deep .amap-copyright {
  248. opacity: 0;
  249. }
  250. </style>
  251. <style lang="scss" scoped>
  252. .m_tools {
  253. position: absolute;
  254. z-index: 999;
  255. // left: 550px;
  256. // top: 120px;
  257. left: 85%;
  258. top: 1px;
  259. width: auto;
  260. color: #409eff;
  261. overflow: hidden;
  262. }
  263. </style>