mapPreview.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. <template>
  2. <map v-if='isMap' id="test1_map" ref="test1_map" :polyline="polyline" :scale="scale"
  3. :style="'width: 100%; border-radius: 28rpx;height:'+height" :markers="markers" :polygons="polygons"
  4. @updated="updatedMap" :latitude="map.latitude" :longitude="map.longitude" show-compass="ture" show-location="true" >
  5. </map>
  6. </template>
  7. <script>
  8. import {
  9. pointData
  10. } from "@/api/company/path.js"
  11. var _this;
  12. export default {
  13. props: {
  14. height: {
  15. type: String,
  16. default: '513rpx'
  17. },
  18. getLocation: {
  19. type: Object,
  20. default: null
  21. },
  22. // getEndLocation: {
  23. // type: Object,
  24. // default: null
  25. // },
  26. startPoint: {
  27. type: String,
  28. default: '',
  29. },
  30. endPoint: {
  31. type: String,
  32. default: '',
  33. },
  34. mtRoutePointList: {
  35. type: Array,
  36. default: () => {
  37. return []
  38. },
  39. },
  40. fwPointList: {
  41. type: Array,
  42. default: () => {
  43. return []
  44. },
  45. },
  46. pathDataId: {
  47. type: String,
  48. default: '',
  49. },
  50. //是否只有点击事件
  51. isDot: {
  52. type: Boolean,
  53. default: false,
  54. },
  55. // //是否是记录
  56. // isInfo: {
  57. // type: Boolean,
  58. // default: true,
  59. // },
  60. },
  61. data() {
  62. return {
  63. barHeight: null, //动态高度初始值
  64. windowHeight: null,
  65. latitude: 39.998204,
  66. longitude: 116.481053,
  67. map:{
  68. latitude: null,
  69. longitude: null,
  70. },
  71. // latitude: '',
  72. // longitude: '',
  73. markers: [],
  74. polygons: [{
  75. points: [
  76. // {
  77. // longitude: "116.477462800",
  78. // latitude: "39.994629400"
  79. // },
  80. // {
  81. // longitude: "116.475796400",
  82. // latitude: "39.999226200"
  83. // },
  84. // {
  85. // longitude: "116.486139200",
  86. // latitude: "40.002975600"
  87. // },
  88. // {
  89. // longitude: "116.487805600",
  90. // latitude: "39.998378800"
  91. // },
  92. ],
  93. fillColor: '#376af64d',
  94. width: 5,
  95. strokeColor: '#9BC2FC', //描边颜色
  96. strokeWidth: 2, //描边宽度
  97. zIndex: 100, //层级
  98. }],
  99. polyline: [{
  100. points: [
  101. // {
  102. // "longitude": 116.478928,
  103. // "latitude": 39.997761
  104. // },
  105. // {
  106. // "longitude": 116.478907,
  107. // "latitude": 39.998422
  108. // },
  109. // {
  110. // "longitude": 116.479384,
  111. // "latitude": 39.998546
  112. // },
  113. // {
  114. // "longitude": 116.481053,
  115. // "latitude": 39.998204
  116. // },
  117. // {
  118. // "longitude": 116.481793,
  119. // "latitude": 39.997868
  120. // },
  121. // {
  122. // "longitude": 116.482898,
  123. // "latitude": 39.998217
  124. // },
  125. // {
  126. // "longitude": 116.483789,
  127. // "latitude": 39.999063
  128. // },
  129. // {
  130. // "longitude": 116.484674,
  131. // "latitude": 39.999844
  132. // }
  133. ],
  134. arrowLine: true,
  135. color: 'rgba(255, 0, 0, 1)',
  136. width: 22,
  137. borderColor: 'rgba(255, 0, 0, 1)',
  138. borderWidth: 20,
  139. dottedLine: true,
  140. }],
  141. startMarker:null,
  142. endMarker:null,
  143. isMap:true,
  144. scale:null,
  145. }
  146. },
  147. computed: {},
  148. onLoad() {
  149. // this.ScanAudio();
  150. // this.fitNav()
  151. // this.getLocation();
  152. },
  153. watch: {
  154. getLocation: {
  155. handler(newData, oldName) {
  156. if (newData != null) {
  157. this.startingPoint(newData.latitude, newData.longitude);
  158. }
  159. },
  160. immediate: true,
  161. deep: true
  162. },
  163. startPoint: {
  164. handler(newData, oldName) {
  165. if (newData != null) {
  166. console.log(newData,'<--------开始点')
  167. let opt=this.mercatorToWGS84(newData);
  168. this.startingPoint(opt.latitude, opt.longitude);
  169. }
  170. },
  171. immediate: true,
  172. deep: true
  173. },
  174. endPoint: {
  175. handler(newData, oldName) {
  176. if (newData != null) {
  177. console.log(newData,'<--------结束点')
  178. let opt=this.mercatorToWGS84(newData);
  179. this.endingPoint(opt.latitude, opt.longitude);
  180. }
  181. },
  182. immediate: true,
  183. deep: true
  184. },
  185. mtRoutePointList(){
  186. this.getMap();
  187. }
  188. },
  189. mounted() {
  190. let that=this;
  191. if(this.isDot)
  192. {
  193. var maps = uni.createMapContext("test1_map", this).$getAppMap();
  194. maps.onclick = function(point) {
  195. console.log(point,'我是点击事件')
  196. that.$emit('dot')
  197. };
  198. }else{
  199. this.isMap=false;
  200. // this.$nextTick(() => {
  201. // })
  202. //解决频繁刷新无法现在最新的中心点
  203. setTimeout(() => {
  204. //需要定时执行的代码
  205. that.getMap();
  206. // console.debug("Hello World");
  207. }, 500)
  208. }
  209. },
  210. methods: {
  211. getfwPointList(pointList){
  212. console.log(pointList,'<-------------pointList');
  213. this.polygons = [{
  214. points: pointList,
  215. fillColor: '#376af64d',
  216. width: 5,
  217. strokeColor: '#9BC2FC', //描边颜色
  218. strokeWidth: 2, //描边宽度
  219. zIndex: 100, //层级
  220. }]
  221. this.polyline = [{
  222. points: [this.getLocation,this.getEndLocation],
  223. arrowLine: true,
  224. color: 'rgba(255, 0, 0, 1)',
  225. width: 22,
  226. borderColor: 'rgba(255, 0, 0, 1)',
  227. borderWidth: 20,
  228. dottedLine: true,
  229. }];
  230. },
  231. // 规划路线的时候给起点一个marker,
  232. startingPoint(latitude, longitude) {
  233. console.log(latitude, longitude, '我是开始坐标');
  234. this.startMarker = {
  235. id: 1,
  236. width: 110,
  237. height: 156,
  238. latitude: latitude,
  239. longitude: longitude,
  240. iconPath: "/static/images/path/star.png",
  241. anchor: {
  242. x: 0.5,
  243. y: 1,
  244. },
  245. };
  246. this.map.longitude = longitude;
  247. this.map.latitude = latitude;
  248. this.markers = [this.startMarker,this.endMarker];
  249. },
  250. endingPoint(latitude, longitude) {
  251. console.log(latitude, longitude, '我是开始坐标');
  252. this.endMarker = {
  253. id: 1,
  254. width: 82,
  255. height: 95,
  256. latitude: latitude,
  257. longitude: longitude,
  258. iconPath: "/static/images/path/end.png",
  259. anchor: {
  260. x: 0.5,
  261. y: 1,
  262. },
  263. };
  264. this.map.longitude = longitude;
  265. this.map.latitude = latitude;
  266. this.markers = [this.startMarker,this.endMarker];
  267. },
  268. updatedMap() {
  269. console.log('地图更新完成', '2222222');
  270. },
  271. async getMap() {
  272. let _this=this;
  273. if(!this.startPoint||!this.endPoint)
  274. {
  275. console.log('返回了');
  276. this.isMap=true;
  277. return false;
  278. }
  279. let startPointData = this.mercatorToWGS84(this.startPoint);
  280. let endPointData = this.mercatorToWGS84(this.endPoint);
  281. this.markers = [{
  282. id: 1,
  283. width: 110,
  284. height: 156,
  285. latitude: startPointData.latitude,
  286. longitude: startPointData.longitude,
  287. iconPath: "/static/images/path/star.png",
  288. anchor: {
  289. x: 0.5,
  290. y: 0.5,
  291. }
  292. },
  293. {
  294. id: 2,
  295. width: 82,
  296. height: 95,
  297. latitude: endPointData.latitude,
  298. longitude: endPointData.longitude,
  299. iconPath: "/static/images/path/end.png",
  300. anchor: {
  301. x: 0.5,
  302. y: 1,
  303. },
  304. }
  305. ]
  306. if (this.mtRoutePointList) {
  307. // console.log(this.mtRoutePointList,'this.mtRoutePointList');
  308. let pointsData = this.mtRoutePointList.map(item => this.mercatorToWGS84(item.pointCoordinate))
  309. //计算中心点
  310. this.latitude = eval(startPointData.latitude + '+' + endPointData.latitude) / 2
  311. this.longitude = eval(startPointData.longitude + '+' + endPointData.longitude) / 2
  312. console.log('中心点', this.latitude, this.longitude)
  313. console.log('开始', startPointData.latitude, startPointData.longitude)
  314. console.log('结束', endPointData.latitude, endPointData.longitude)
  315. this.polygons = [{
  316. points: pointsData,
  317. fillColor: '#376af64d',
  318. width: 5,
  319. strokeColor: '#9BC2FC', //描边颜色
  320. strokeWidth: 2, //描边宽度
  321. zIndex: 100, //层级
  322. }]
  323. //计算缩放比例
  324. // _this.$nextTick(()=>{
  325. _this.map.latitude=_this.latitude;
  326. _this.map.longitude=_this.longitude;
  327. _this.scale = _this.scaleDW(pointsData);
  328. // })
  329. if (this.pathDataId) {
  330. let resData = await pointData(this.pathDataId)
  331. // console.log(resData,'resData');
  332. let pointsData2 = resData.data.runPoints.map(item => {
  333. return {
  334. latitude: item[1],
  335. longitude: item[0]
  336. }
  337. });
  338. // pointsData.unshift(startPointData);
  339. // pointsData.push(endPointData);
  340. console.log(pointsData2,'<---------------我是点位');
  341. // if(this.isInfo)
  342. // {
  343. this.markers = [{
  344. id: 1,
  345. width: 110,
  346. height: 156,
  347. latitude: startPointData.latitude,
  348. longitude: startPointData.longitude,
  349. iconPath: "/static/images/path/rstar.png",
  350. anchor: {
  351. x: 0.5,
  352. y: 0.5,
  353. }
  354. },
  355. {
  356. id: 2,
  357. width: 82,
  358. height: 95,
  359. latitude: endPointData.latitude,
  360. longitude: endPointData.longitude,
  361. iconPath: "/static/images/path/rend.png",
  362. anchor: {
  363. x: 0.5,
  364. y: 1,
  365. },
  366. },
  367. {
  368. id: 3,
  369. width: 82,
  370. height: 95,
  371. latitude: pointsData2[0].latitude,
  372. longitude: pointsData2[0].longitude,
  373. iconPath: "/static/images/path/star.png",
  374. anchor: {
  375. x: 0.5,
  376. y: 1,
  377. },
  378. },
  379. {
  380. id: 4,
  381. width: 82,
  382. height: 95,
  383. latitude: pointsData2[pointsData2.length-1].latitude,
  384. longitude: pointsData2[pointsData2.length-1].longitude,
  385. iconPath: "/static/images/path/end.png",
  386. anchor: {
  387. x: 0.5,
  388. y: 1,
  389. },
  390. }
  391. ]
  392. this.polyline = [
  393. {
  394. points: [startPointData,endPointData],
  395. arrowLine: true,
  396. color: '#00CC6A',
  397. width: 22,
  398. borderColor: '#00CC6A',
  399. borderWidth: 20,
  400. dottedLine: true,
  401. },
  402. {
  403. points: pointsData2,
  404. arrowLine: true,
  405. color: 'rgba(255, 0, 0, 1)',
  406. width: 22,
  407. borderColor: 'rgba(255, 0, 0, 1)',
  408. borderWidth: 20,
  409. dottedLine: true,
  410. },
  411. ];
  412. // }else
  413. // {
  414. // this.polyline = [{
  415. // points: pointsData2,
  416. // arrowLine: true,
  417. // color: 'rgba(255, 0, 0, 1)',
  418. // width: 22,
  419. // borderColor: 'rgba(255, 0, 0, 1)',
  420. // borderWidth: 20,
  421. // dottedLine: true,
  422. // }];
  423. // }
  424. // this.map.latitude=this.latitude;
  425. // this.map.longitude=this.longitude;
  426. }else{
  427. this.polyline = [{
  428. points: [startPointData,endPointData],
  429. arrowLine: true,
  430. // color: 'rgba(255, 0, 0, 1)',
  431. // width: 22,
  432. // borderColor: 'rgba(255, 0, 0, 1)',\
  433. color: '#00CC6A',
  434. width: 22,
  435. borderColor: '#00CC6A',
  436. borderWidth: 20,
  437. dottedLine: true,
  438. }];
  439. }
  440. // 处理中心点不变的问题
  441. // setTimeout(() => {
  442. // _this.map.latitude=_this.latitude;
  443. // _this.map.longitude=_this.longitude;
  444. // }, 500)
  445. }
  446. this.isMap=true;
  447. },
  448. scaleDW(otherPoints) {
  449. let centralPoint = []
  450. centralPoint[0] = this.latitude
  451. centralPoint[1] = this.longitude
  452. let obj = {
  453. centralPoint: [this.latitude, this.longitude],
  454. otherPoint: otherPoints
  455. }
  456. console.log(this.furthest(obj),'<---------------我是缩放的等比')
  457. if(this.furthest(obj)>12&&this.furthest(obj)<20)
  458. {
  459. return this.furthest(obj)+1
  460. }
  461. return this.furthest(obj)
  462. },
  463. furthest(obj) {
  464. // console.log(obj)
  465. var EARTH_RADIUS = 6378137.0; //单位M
  466. var PI = Math.PI;
  467. // 转为角度
  468. function getRad(d) {
  469. return d * PI / 180.0;
  470. }
  471. let distance = {
  472. // 将地球按照圆形计算
  473. getGreatCircle: function(coordinate1, coordinate2) {
  474. var lat1 = coordinate1[0] // 纬度
  475. var lat2 = coordinate2[0]
  476. var lng1 = coordinate1[1] // 经度
  477. var lng2 = coordinate2[1]
  478. var radLat1 = getRad(lat1);
  479. var radLat2 = getRad(lat2);
  480. var a = radLat1 - radLat2;
  481. var b = getRad(lng1) - getRad(lng2);
  482. var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(
  483. radLat1) *
  484. Math
  485. .cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
  486. s = s * EARTH_RADIUS;
  487. s = Math.round(s * 10000) / 10000.0;
  488. return s;
  489. },
  490. // 将地球按照椭圆进行计算
  491. getFlattern: function(coordinate1, coordinate2) {
  492. var lat1 = coordinate1[0] // 纬度
  493. var lat2 = coordinate2[0]
  494. var lng1 = coordinate1[1] // 经度
  495. var lng2 = coordinate2[1]
  496. var f = getRad((lat1 + lat2) / 2);
  497. var g = getRad((lat1 - lat2) / 2);
  498. var l = getRad((lng1 - lng2) / 2);
  499. var sg = Math.sin(g);
  500. var sl = Math.sin(l);
  501. var sf = Math.sin(f);
  502. var s, c, w, r, d, h1, h2;
  503. var a = EARTH_RADIUS;
  504. var fl = 1 / 298.257;
  505. sg = sg * sg;
  506. sl = sl * sl;
  507. sf = sf * sf;
  508. s = sg * (1 - sl) + (1 - sf) * sl;
  509. c = (1 - sg) * (1 - sl) + sf * sl;
  510. w = Math.atan(Math.sqrt(s / c));
  511. r = Math.sqrt(s * c) / w;
  512. d = 2 * w * a;
  513. h1 = (3 * r - 1) / 2 / c;
  514. h2 = (3 * r + 1) / 2 / s;
  515. return d * (1 + fl * (h1 * sf * (1 - sg) - h2 * (1 - sf) * sg));
  516. }
  517. }
  518. // obj包含中心点坐标和其他所有的坐标位置
  519. // 处理obj传入的参数
  520. let centralPoint = obj.centralPoint // 中心点
  521. let otherPoint = obj.otherPoint // 二维数组存放其他坐标点
  522. // 循环遍历得出距离最远的1个坐标然后*2
  523. let farthestCoordinate = [] // 当前最远坐标
  524. let farthestDistance = 0 // 最远距离
  525. let dis = null // 存储当前距离
  526. for (let i in otherPoint) {
  527. let newLocation = []
  528. newLocation[0] = otherPoint[i].latitude
  529. newLocation[1] = otherPoint[i].longitude
  530. dis = distance.getGreatCircle(centralPoint, newLocation)
  531. if (dis > farthestDistance) {
  532. farthestDistance = dis
  533. farthestCoordinate = newLocation
  534. }
  535. }
  536. farthestDistance = farthestDistance / 1000 // 转为km
  537. // 根据坐标计算出合理的比例尺寸
  538. let surface = [500, 200, 100, 50, 20, 10, 5, 2, 1, 0.5, 0.2, 0.1, 0.05, 0.02]
  539. for (let i in surface) {
  540. if (farthestDistance > surface[i]) {
  541. let scale = 5 + Number(i)
  542. return scale
  543. }
  544. }
  545. return 16
  546. },
  547. mercatorToWGS84(data) {
  548. let list = data.split(',');
  549. let lonlat = {
  550. lng: list[1],
  551. lat: list[0]
  552. };
  553. const coord = {
  554. lat: 0,
  555. lng: 0
  556. };
  557. const lat = lonlat.lat / 20037508.34 * 180;
  558. let lng = lonlat.lng / 20037508.34 * 180;
  559. lng = 180 / Math.PI * (2 * Math.atan(Math.exp(lng * Math.PI / 180)) - Math.PI / 2);
  560. coord.lat = lat;
  561. coord.lng = lng;
  562. return {
  563. latitude: coord.lng,
  564. longitude: coord.lat
  565. }
  566. },
  567. }
  568. }
  569. </script>