park.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. <template>
  2. <view>
  3. <map id="map" :longitude="longitude" :latitude="latitude" :polyline="polyline" scale="14" :polygons="polygons"
  4. show-location style="width: 100%; height:100vh;" :markers="markers" @markertap="markertap" :data-markers="markers"
  5. :enable-satellite="satellite" @controltap="controltap" :controls="controls">
  6. <cover-view @click="again" v-if="mapclick" class="again" style="left: 5%; border-radius: 15upx;">重绘</cover-view>
  7. <cover-view @click="cancelBtn" v-if="mapclick" class="cancelBtn">取消</cover-view>
  8. <cover-view @click="finish" v-if="mapclick" class="finish">完成</cover-view>
  9. <view class="info" v-if="markShow">
  10. <view class="infoCon">
  11. <view class='flexB'>
  12. <view style="font-size:32upx;">区域名称:{{detail.name}}</view>
  13. <view class="flex" style="margin-top:15upx;align-items:center;">
  14. <!-- <van-switch style="margin:0 15upx;" checked="detail.status==1 ? true:false " size="18px" bind:change="switchCon"
  15. active-color="#07c160" inactive-color="#f00" /> -->
  16. <text style="margin-bottom:7upx;">{{detail.status==1 ? '已开启':'已禁用'}}</text>
  17. </view>
  18. </view>
  19. <view class="deleted" @click="remove" :data-detail="detail">
  20. <text>删除</text>
  21. </view>
  22. </view>
  23. </view>
  24. <cover-image src="http://resource.weilaibike.com/bike_yunwei/ref.png" @click="refresh" class="ref" style="bottom: 17%;"></cover-image>
  25. <cover-image src="http://resource.weilaibike.com/change.png" @click="sateposi" class="satellite flexCC"></cover-image>
  26. <cover-view id="top" @click="push" v-if="!mapclick" style="background: #18d5b9">添加区域</cover-view>
  27. <cover-image src="http://resource.weilaibike.com/bike_yunwei/location.png" @click="posi" class="position"></cover-image>
  28. </map>
  29. </view>
  30. </template>
  31. <script>
  32. const app = getApp();
  33. export default {
  34. data() {
  35. return {
  36. latitude: "",
  37. longitude: "",
  38. markers: [], //点
  39. polyline: [], //线
  40. polygons: [], //区域
  41. controls: [],
  42. list: [],
  43. fence: [], //点击地图获取的经纬度
  44. type: '', //停车区类型
  45. parkName: '', //停车区名称
  46. markShow: false, //是否显示停车点名称
  47. parkId: '', //停车点/禁停区id
  48. detail: [],
  49. showPop: false, //是否显示弹窗
  50. areaList: [], //选择所属大区列表
  51. typeList: [{ //区域类型选项
  52. type: 1,
  53. name: '禁停区'
  54. }, {
  55. type: 2,
  56. name: '停车区'
  57. }],
  58. options: '',
  59. parkNum: 0, //停车上限
  60. mapclick: false, //是否执行地图点击事件
  61. areaChecked: true, //弹窗区域的状态
  62. areaId: '', //区域id
  63. btnShow: false, //重绘 取消
  64. pushShow: true,
  65. areaName: '',
  66. satellite: false, //是否显示卫星地图
  67. newArea: {}, // 新添加的停车点或者禁停区
  68. areaID: 0,
  69. value1: 0, //第一个显示什么
  70. option1: [],
  71. rideArea: [], //骑行区
  72. screenHeight: '', //屏幕可视高度
  73. showArea: '',
  74. showType: ''
  75. }
  76. },
  77. methods: {
  78. //重绘
  79. again: function() {
  80. console.log('重绘')
  81. this.getArea();
  82. this.polygons = [];
  83. this.markers = [];
  84. this.polyline = [];
  85. this.list = [];
  86. this.btnShow = false;
  87. this.park();
  88. },
  89. cancelBtn: function() {
  90. console.log('取消')
  91. this.mapclick = false;
  92. this.markers = [];
  93. this.pushShow = true;
  94. this.controls = []
  95. this.polygons = []
  96. this.polyline = []
  97. this.list = []
  98. this.park();
  99. },
  100. //完成
  101. finish: function() {
  102. let that = this;
  103. if (that.fence.length >= 3) {
  104. let state = JSON.parse(that.areaChecked);
  105. let parking_fence = []
  106. that.fence.map(i => {
  107. parking_fence.push({
  108. lat: i.latitude,
  109. lng: i.longitude
  110. })
  111. })
  112. if (state) {
  113. state = 1
  114. } else {
  115. state = 0
  116. }
  117. let data = {
  118. parking_fence: JSON.stringify(parking_fence),
  119. area_id: that.areaId,
  120. max_number: that.parkNum,
  121. name: that.parkName,
  122. status: state,
  123. type: that.type,
  124. }
  125. app.request('parking', data, 'POST').then(res => {
  126. if (res.statusCode == 201) {
  127. console.log(that.type, 555)
  128. console.log(res, '添加');
  129. uni.showToast({
  130. title: '添加成功',
  131. icon: 'none',
  132. duration: 3000,
  133. success: function() {
  134. that.mapclick = false;
  135. that.pushShow = true;
  136. that.controls = [];
  137. that.list = [];
  138. that.polygons = [];
  139. that.polyline = [];
  140. that.markers = [];
  141. that.park();
  142. }
  143. })
  144. }
  145. })
  146. } else {
  147. uni.showToast({
  148. title: '请至少选择三个点',
  149. icon: 'none'
  150. })
  151. }
  152. },
  153. //添加还车点、禁停区
  154. push: function() {
  155. // this.showPop = true;
  156. // this.parkName = '';
  157. // this.parkNum = 0;
  158. // this.areaChecked = true;
  159. // this.markShow = false;
  160. // this.areaName = this.option1[0].text;
  161. // this.typeName = this.typeList[1].name;
  162. // this.type = this.typeList[1].type;
  163. // this.areaId = this.option1[0].areaID;
  164. uni.navigateTo({
  165. url: "./add_park"
  166. })
  167. },
  168. posi: function() {
  169. //点击定位
  170. console.log(12)
  171. uni.getLocation({
  172. type: 'gcj02',
  173. success: (res) => {
  174. console.log(res)
  175. this.longitude = res.longitude;
  176. this.latitude = res.latitude;
  177. }
  178. })
  179. },
  180. refresh: function() {
  181. uni.showLoading({
  182. title: '刷新中...',
  183. })
  184. this.markShow = false;
  185. this.park();
  186. },
  187. sateposi: function() {
  188. this.satellite = !this.satellite
  189. },
  190. //选择区域类型
  191. choiceType: function() {
  192. this.showType = !this.showType;
  193. },
  194. // 改变区域状态
  195. changeState: function() {
  196. this.areaChecked = !this.areaChecked;
  197. },
  198. //绘制围栏
  199. draw: function() {
  200. let that = this;
  201. uni.getSystemInfo({
  202. success: (res) => {
  203. console.log(1111)
  204. this.controls = [{
  205. id: 1,
  206. iconPath: '/static/img/lock.png', // 中心点图片
  207. position: {
  208. left: res.windowWidth / 2 - 9,
  209. top: res.windowHeight / 2 - 27,
  210. width: 45,
  211. height: 45
  212. },
  213. clickable: true
  214. }]
  215. }
  216. })
  217. this.showPop = false;
  218. this.mapclick = true;
  219. this.pushShow = false;
  220. uni.showToast({
  221. title: '请点击地图进行绘制',
  222. icon: 'none'
  223. })
  224. },
  225. //地图上绘制停车区
  226. controltap: function(e) {
  227. let that = this;
  228. let mapCtx = uni.createMapContext("map");
  229. let arr = that.list;
  230. mapCtx.getCenterLocation({
  231. success: function(res) {
  232. console.log(res.latitude, '经纬度')
  233. var latitude = res.latitude
  234. var longitude = res.longitude
  235. var color = '';
  236. var bgColor = '';
  237. var iconPath = '';
  238. if (that.type == 1) { //禁停区
  239. iconPath = "http://resource.weilaibike.com/redStop.png";
  240. color = "#FF0000";
  241. bgColor = "#FF000002";
  242. } else if (that.type == 2) { //停车区
  243. iconPath = "http://resource.weilaibike.com/stop1.png";
  244. color = "#ff00ff";
  245. bgColor = "#ff00ff02"
  246. }
  247. let obj = {
  248. latitude,
  249. longitude
  250. };
  251. arr.push(obj);
  252. console.log(arr, 'arr')
  253. if (arr.length == 1) {
  254. console.log('1')
  255. let dot = {}
  256. dot.latitude = arr[0].latitude;
  257. dot.longitude = arr[0].longitude;
  258. dot.width = 28;
  259. dot.height = 28;
  260. dot.iconPath = iconPath;
  261. let dotArr = [];
  262. dotArr.push(dot)
  263. that.markers = that.markers.concat(dotArr)
  264. }
  265. if (arr.length == 2) {
  266. console.log('2')
  267. let line = {};
  268. line.points = arr;
  269. line.width = 4;
  270. line.borderWidth = 3;
  271. line.borderColor = color;
  272. line.color = color;
  273. let lineArr = [];
  274. lineArr.push(line);
  275. console.log(lineArr)
  276. that.markers.splice(that.markers.length - 1, 1)
  277. that.polyline = lineArr;
  278. that.markers = that.markers;
  279. // console.log(lineArr)
  280. }
  281. if (arr.length == 3) {
  282. console.log('==3')
  283. let line = {};
  284. line.points = arr;
  285. let lineArr = [];
  286. lineArr.push(line);
  287. let area = {};
  288. that.fence = arr;
  289. that.btnShow = true;
  290. area.points = arr;
  291. let areaArr = [];
  292. areaArr.push(area)
  293. console.log(areaArr)
  294. var areaPoly = areaArr[0].points[0];
  295. var areaPons = areaArr[0].points.concat(areaPoly);
  296. console.log(areaPons)
  297. var obj2 = {
  298. 'points':areaPons,
  299. 'fillColor':bgColor,
  300. 'strokeColor':color,
  301. 'strokeWidth':4,
  302. 'zIndex':1,
  303. 'id':99
  304. }
  305. that.polygons = that.polygons.concat(obj2);
  306. var poly = lineArr[0].points[0];
  307. var polylines = lineArr[0].points.concat(poly);
  308. var obj1 = {
  309. 'points':polylines,
  310. 'width':4,
  311. 'borderWidth':3,
  312. 'borderColor':color,
  313. 'color':color
  314. }
  315. var arr1 = []
  316. arr1.push(obj1)
  317. that.polyline = arr1;
  318. console.log(that.polyline)
  319. console.log(that.polygons)
  320. }
  321. if(arr.length>=4){
  322. console.log(that.polygons)
  323. var arr5 = that.polygons[0].points;
  324. var arr6 = that.deWeight(arr5)
  325. that.polygons[0].points = arr6
  326. console.log(that.polygons)
  327. let line = {};
  328. line.points = arr;
  329. let lineArr = [];
  330. lineArr.push(line);
  331. let area = {};
  332. that.fence = arr;
  333. that.btnShow = true;
  334. area.points = arr;
  335. let areaArr = [];
  336. areaArr.push(area)
  337. console.log(areaArr)
  338. var areaPoly = areaArr[0].points[0];
  339. var areaPons = areaArr[0].points.concat(areaPoly);
  340. console.log(areaPons)
  341. var obj2 = {
  342. 'points':areaPons,
  343. 'fillColor':bgColor,
  344. 'strokeColor':color,
  345. 'strokeWidth':4,
  346. 'zIndex':1,
  347. 'id':99
  348. }
  349. var poly2 = that.polygons.concat(obj2)
  350. that.polygons = poly2;
  351. var poly = lineArr[0].points[0];
  352. var polylines = lineArr[0].points.concat(poly);
  353. var obj1 = {
  354. 'points':polylines,
  355. 'width':4,
  356. 'borderWidth':3,
  357. 'borderColor':color,
  358. 'color':color
  359. }
  360. var arr1 = []
  361. arr1.push(obj1)
  362. that.polyline = arr1;
  363. console.log(that.polyline)
  364. console.log(that.polygons)
  365. }
  366. }
  367. })
  368. },
  369. //停车点和禁停点
  370. park: function() {
  371. uni.showLoading({
  372. title: '加载中...',
  373. })
  374. var that = this;
  375. console.log(that.areaID)
  376. let url = 'parking?area_id=1'
  377. that.getArea();
  378. app.request(url, '', 'GET').then(res => {
  379. uni.hideLoading();
  380. // console.log(res)
  381. let polygons = res.data.parking_area.concat(that.rideArea);
  382. that.markers = res.data.point;
  383. that.polygons = polygons;
  384. that.polyline = that.polygons;
  385. console.log(that.polygons, '停车区和禁停区')
  386. console.log(that.markers, 'markers')
  387. })
  388. },
  389. deWeight:function(arr) {
  390. for (var i = 0; i < arr.length - 1; i++) {
  391. for (var j = i + 1; j < arr.length; j++) {
  392. if (arr[i].longitude == arr[j].longitude) {
  393. arr.splice(j, 1);
  394. //因为数组长度减小1,所以直接 j++ 会漏掉一个元素,所以要 j--
  395. j--;
  396. }
  397. }
  398. }
  399. return arr;
  400. },
  401. // 获取骑行区
  402. getArea: function() {
  403. var that = this;
  404. let url = 'getAreas?put_area_id=' + that.areaID + "&&is_with_parking=" + 0
  405. app.request(url, '', 'GET').then(res => {
  406. console.log(res);
  407. that.rideArea = res.data.ridding_area
  408. })
  409. },
  410. //停车(禁停)图标点击事件
  411. markertap: function(e) {
  412. console.log(e)
  413. this.getPark_info(e.detail.markerId)
  414. this.parkId = e.detail.markerId;
  415. },
  416. //获取停车(禁停)点信息
  417. getPark_info: function(parkId) {
  418. var that = this;
  419. uni.showLoading({
  420. title: '加载中...',
  421. })
  422. console.log(parkId)
  423. app.request('parking/' + parkId.slice(1), '', 'GET').then(res => {
  424. uni.hideLoading();
  425. console.log(res.data)
  426. this.detail = res.data;
  427. this.markShow = true;
  428. var status = ''
  429. if (res.data.status == 1) {
  430. status = '已启用'
  431. } else {
  432. status = '已禁用'
  433. }
  434. console.log(status, 'status')
  435. uni.showActionSheet({
  436. itemList: ['禁用/启用,当前' + status, '删除', '取消'],
  437. success: function(res) {
  438. console.log('选中了第' + (res.tapIndex + 1) + '个按钮');
  439. if (res.tapIndex + 1 == 2) {
  440. //删除
  441. that.remove()
  442. }
  443. if (res.tapIndex + 1 == 1) {
  444. //禁用或者启用
  445. that.switchCon()
  446. }
  447. },
  448. fail: function(res) {
  449. console.log(res.errMsg);
  450. }
  451. });
  452. })
  453. },
  454. //删除当前停车(禁停点)
  455. remove: function(e) {
  456. console.log(e);
  457. var that = this;
  458. console.log(that.parkId, 'parkId')
  459. uni.showModal({
  460. title: '提示',
  461. content: '确定删除该区域吗?',
  462. success: function(res) {
  463. if (res.confirm) {
  464. app.request('parking/' + that.parkId.slice(1), '', 'DELETE').then(res => {
  465. if (res.statusCode == 204) {
  466. that.park();
  467. uni.showToast({
  468. title: '删除成功',
  469. icon: 'none',
  470. duration: 2000,
  471. })
  472. that.markShow = false;
  473. }
  474. })
  475. }
  476. }
  477. })
  478. },
  479. //停车区状态
  480. switchCon: function() {
  481. uni.showLoading({
  482. title: '修改中...',
  483. })
  484. var that = this;
  485. console.log(that)
  486. let detail = that.detail
  487. detail.status = !detail.status;
  488. let status = '';
  489. if (detail.status == true) {
  490. status = 1
  491. } else {
  492. status = 0
  493. }
  494. that.detail = detail;
  495. let data = {
  496. status,
  497. id: that.parkId
  498. }
  499. app.request('parking/status', data, 'POST').then(res => {
  500. uni.hideLoading();
  501. if (res.statusCode == 200) {
  502. uni.showToast({
  503. title: '修改成功',
  504. icon: 'none'
  505. })
  506. }
  507. })
  508. },
  509. },
  510. onLoad(options) {
  511. this.park(); // 获取停车区
  512. this.getArea(); //获取骑行区
  513. // this.controltap()
  514. },
  515. onShow: function() {
  516. console.log(this.areaId)
  517. console.log(this.parkName)
  518. console.log(this.parkNum)
  519. console.log(this.type)
  520. if (this.mapclick) {
  521. this.draw()
  522. }
  523. uni.getLocation({
  524. type: 'gcj02',
  525. success: (res) => {
  526. console.log(res)
  527. this.longitude = res.longitude;
  528. this.latitude = res.latitude;
  529. }
  530. })
  531. }
  532. }
  533. </script>
  534. <style>
  535. .info {
  536. font-size: 28upx;
  537. width: 100%;
  538. height: 76upx;
  539. position: absolute;
  540. left: 0;
  541. top: 12%;
  542. }
  543. .info .infoCon {
  544. background: #fff;
  545. width: 90%;
  546. margin: 0 auto;
  547. padding: 10upx;
  548. border-radius: 10upx;
  549. display: flex;
  550. justify-content: space-between;
  551. align-items: center;
  552. }
  553. .info .infoCon .flexB {
  554. display: flex;
  555. flex-direction: column;
  556. align-items: flex-start;
  557. }
  558. .info .deleted {
  559. display: flex;
  560. align-items: center;
  561. justify-content: center;
  562. background: #f00;
  563. color: #fff;
  564. font-size: 26upx;
  565. padding: 10upx 30upx;
  566. border-radius: 25upx;
  567. margin-right: 15upx;
  568. height: 35upx;
  569. }
  570. #top {
  571. position: absolute;
  572. width: 300upx;
  573. height: 70upx;
  574. z-index: 999;
  575. bottom: 17%;
  576. left: 50%;
  577. margin-left: -150upx;
  578. border-radius: 15upx;
  579. background: #18d5b9;
  580. font-size: 28upx;
  581. color: #fff;
  582. text-align: center;
  583. line-height: 60upx;
  584. }
  585. .position,
  586. .ref {
  587. position: absolute;
  588. left: 10%;
  589. bottom: 27%;
  590. display: flex;
  591. align-items: center;
  592. justify-content: center;
  593. height: 72upx;
  594. width: 72upx;
  595. }
  596. .satellite {
  597. position: absolute;
  598. left: 80%;
  599. bottom: 17%;
  600. height: 40upx;
  601. width: 40upx;
  602. }
  603. .again {
  604. position: absolute;
  605. bottom: 100upx;
  606. width: 27%;
  607. height: 70upx;
  608. line-height: 60upx;
  609. text-align: center;
  610. background: #18d5b9;
  611. color: white;
  612. font-size: 30upx;
  613. border-radius: 15upx;
  614. z-index: 999;
  615. }
  616. .cancelBtn {
  617. width: 27%;
  618. border-radius: 15upx;
  619. position: absolute;
  620. bottom: 100upx;
  621. height: 70upx;
  622. left: 37% !important;
  623. font-size: 30upx;
  624. background: #18d5b9;
  625. color: #FFFFFF;
  626. text-align: center;
  627. line-height: 60upx;
  628. }
  629. .finish {
  630. border-radius: 15upx;
  631. width: 27%;
  632. position: absolute;
  633. bottom: 100upx;
  634. font-size: 30upx;
  635. height: 70upx;
  636. right: 5% !important;
  637. color: #FFFFFF;
  638. text-align: center;
  639. background: #18d5b9;
  640. line-height: 60upx;
  641. }
  642. .bt {
  643. height: 70upx;
  644. line-height: 70upx;
  645. width: 25%;
  646. background-color: #007AFF;
  647. color: #FFFFFF;
  648. border-radius: 10upx;
  649. font-size: 28upx;
  650. }
  651. </style>