mapNew.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. <template>
  2. <view>
  3. <map id="test_map" ref="map1" :style="'width: 100%; border-radius: 28rpx;height:'+height" :scale="scale" :show-location="true"
  4. :longitude="map.longitude" :latitude="map.latitude" :polygons="polygons" :markers="markers"
  5. @updated="updated" :polyline="polyline" show-compass="ture" show-location="true" :controls="controls">
  6. </map>
  7. <!-- <button @click="graph()"> 绘制矩形</button> -->
  8. </view>
  9. </template>
  10. <script>
  11. import {
  12. routePost
  13. } from "@/api/company/path.js"
  14. export default {
  15. props: {
  16. height: {
  17. type: String,
  18. default: '713rpx'
  19. },
  20. isShow: {
  21. type: Boolean,
  22. default: true
  23. },
  24. getLocation: {
  25. type: Object,
  26. default: null,
  27. },
  28. starAddres: {
  29. type: Object,
  30. default: null,
  31. },
  32. isStar: {
  33. type: Boolean,
  34. default: false,
  35. },
  36. isEnd: {
  37. type: Boolean,
  38. default: false,
  39. }
  40. },
  41. watch: {
  42. starAddres(NewData) {
  43. if (NewData&&NewData.location) {
  44. console.log(NewData,'《---------我是监听的点位')
  45. let postion = NewData.location.split(',');
  46. this.graph([]);
  47. // console.log(NewData, 'NewData');
  48. if (this.isStar) {
  49. this.endPoint(postion[1], postion[0], true);
  50. } else {
  51. this.startingPoint(postion[1], postion[0], true);
  52. }
  53. this.$emit('dot')
  54. }
  55. },
  56. getLocation: {
  57. handler(NewData, oldName) {
  58. console.log('我是获取的地址信息----------》',NewData);
  59. let _this = this;
  60. if (NewData != null) {
  61. // console.log("当前位置", this.getLocation);
  62. _this.map.longitude = NewData.longitude;
  63. _this.map.latitude = NewData.latitude;
  64. _this.startingPoint(NewData.latitude, NewData.longitude);
  65. }
  66. },
  67. immediate: true,
  68. deep: true
  69. },
  70. isEnd(NewData) {
  71. console.log(NewData,'《---------我是监听结束的')
  72. if (NewData) {
  73. let data = this.markers.map(item => {
  74. return item.latitude+','+item.longitude
  75. })
  76. // this.path(data);
  77. // let data2 = [{
  78. // latitude: data[0].latitude.slice(0, data[0].latitude.length - 2) + 99,
  79. // longitude: data[0].longitude.slice(0, data[0].longitude.length - 2) + 99,
  80. // },
  81. // {
  82. // latitude: data[0].latitude.slice(0, data[0].latitude.length - 2),
  83. // longitude: data[0].longitude.slice(0, data[0].longitude.length - 2) + 99
  84. // },
  85. // {
  86. // latitude: data[0].latitude.slice(0, data[0].latitude.length - 2),
  87. // longitude: data[0].longitude.slice(0, data[0].longitude.length - 2)
  88. // },
  89. // {
  90. // latitude: data[0].latitude,
  91. // longitude: data[0].longitude.slice(0, data[0].longitude.length - 2),
  92. // },
  93. // ]
  94. let addres={
  95. "startPoint": data[0],
  96. "endPoint": data[1]
  97. }
  98. this.routePost(addres);
  99. }
  100. }
  101. },
  102. data() {
  103. return {
  104. mapContext: {},
  105. map: {
  106. longitude: "126.04684", //经度
  107. latitude: "42.68497", //纬度
  108. },
  109. longitude:"", //经度
  110. latitude:"", //纬度
  111. scale:null,
  112. form: {
  113. name: "",
  114. area: "",
  115. address: "",
  116. location: "",
  117. },
  118. runningRoute: "",
  119. polygons: [{
  120. points: [],
  121. }],
  122. polyline: [{
  123. // 每个点的经纬度
  124. points: [],
  125. arrowLine: true,
  126. color: 'rgba(255, 0, 0, 1)',
  127. width: 20,
  128. borderColor: 'rgba(255, 0, 0, 1)',
  129. borderWidth: 20,
  130. dottedLine: true,
  131. }, ],
  132. controls: [
  133. // {
  134. // //在地图上显示控件,控件不随着地图移动
  135. // id: 1, //控件id
  136. // iconPath: "/static/c1.png", //显示的图标
  137. // position: {
  138. // //控件在地图的位置
  139. // left: 15,
  140. // top: 15,
  141. // width: 50,
  142. // height: 50,
  143. // },
  144. // },
  145. ],
  146. markers: [
  147. // {
  148. // longitude: 118.769032,
  149. // latitude: 31.975258,
  150. // iconPath: "/static/c1.png",
  151. // },
  152. ],
  153. startMarker: null
  154. };
  155. },
  156. created() {
  157. },
  158. mounted() {
  159. console.log('地址信息----------》',this.getLocation);
  160. if (this.isShow) {
  161. } else {
  162. let that = this;
  163. var maps = uni.createMapContext("test_map", this).$getAppMap();
  164. maps.onclick = function(point) {
  165. console.log(that.isStar, 'that.isStar');
  166. console.log(that.isEnd, 'that.isEnd');
  167. that.graph([]);
  168. if (that.isStar && that.isEnd) {
  169. console.log('清空啦');
  170. // that.path([]);
  171. that.startingPoint(point.latitude, point.longitude);
  172. that.$emit('clearMarkers')
  173. } else if (that.isStar) {
  174. that.endPoint(point.latitude, point.longitude);
  175. } else {
  176. that.startingPoint(point.latitude, point.longitude);
  177. }
  178. that.$emit('isChange')
  179. };
  180. }
  181. },
  182. methods: {
  183. scaleDW(otherPoints) {
  184. let centralPoint = []
  185. centralPoint[0] = this.latitude
  186. centralPoint[1] = this.longitude
  187. let obj = {
  188. centralPoint: [this.latitude, this.longitude],
  189. otherPoint: otherPoints
  190. }
  191. console.log(this.furthest(obj),'<---------------我是缩放的等比')
  192. if(this.furthest(obj)>11&&this.furthest(obj)<20)
  193. {
  194. return this.furthest(obj)+1
  195. }
  196. return this.furthest(obj)
  197. },
  198. furthest(obj) {
  199. // console.log(obj)
  200. var EARTH_RADIUS = 6378137.0; //单位M
  201. var PI = Math.PI;
  202. // 转为角度
  203. function getRad(d) {
  204. return d * PI / 180.0;
  205. }
  206. let distance = {
  207. // 将地球按照圆形计算
  208. getGreatCircle: function(coordinate1, coordinate2) {
  209. var lat1 = coordinate1[0] // 纬度
  210. var lat2 = coordinate2[0]
  211. var lng1 = coordinate1[1] // 经度
  212. var lng2 = coordinate2[1]
  213. var radLat1 = getRad(lat1);
  214. var radLat2 = getRad(lat2);
  215. var a = radLat1 - radLat2;
  216. var b = getRad(lng1) - getRad(lng2);
  217. var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(
  218. radLat1) *
  219. Math
  220. .cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
  221. s = s * EARTH_RADIUS;
  222. s = Math.round(s * 10000) / 10000.0;
  223. return s;
  224. },
  225. // 将地球按照椭圆进行计算
  226. getFlattern: function(coordinate1, coordinate2) {
  227. var lat1 = coordinate1[0] // 纬度
  228. var lat2 = coordinate2[0]
  229. var lng1 = coordinate1[1] // 经度
  230. var lng2 = coordinate2[1]
  231. var f = getRad((lat1 + lat2) / 2);
  232. var g = getRad((lat1 - lat2) / 2);
  233. var l = getRad((lng1 - lng2) / 2);
  234. var sg = Math.sin(g);
  235. var sl = Math.sin(l);
  236. var sf = Math.sin(f);
  237. var s, c, w, r, d, h1, h2;
  238. var a = EARTH_RADIUS;
  239. var fl = 1 / 298.257;
  240. sg = sg * sg;
  241. sl = sl * sl;
  242. sf = sf * sf;
  243. s = sg * (1 - sl) + (1 - sf) * sl;
  244. c = (1 - sg) * (1 - sl) + sf * sl;
  245. w = Math.atan(Math.sqrt(s / c));
  246. r = Math.sqrt(s * c) / w;
  247. d = 2 * w * a;
  248. h1 = (3 * r - 1) / 2 / c;
  249. h2 = (3 * r + 1) / 2 / s;
  250. return d * (1 + fl * (h1 * sf * (1 - sg) - h2 * (1 - sf) * sg));
  251. }
  252. }
  253. // obj包含中心点坐标和其他所有的坐标位置
  254. // 处理obj传入的参数
  255. let centralPoint = obj.centralPoint // 中心点
  256. let otherPoint = obj.otherPoint // 二维数组存放其他坐标点
  257. // 循环遍历得出距离最远的1个坐标然后*2
  258. let farthestCoordinate = [] // 当前最远坐标
  259. let farthestDistance = 0 // 最远距离
  260. let dis = null // 存储当前距离
  261. for (let i in otherPoint) {
  262. let newLocation = []
  263. newLocation[0] = otherPoint[i].latitude
  264. newLocation[1] = otherPoint[i].longitude
  265. dis = distance.getGreatCircle(centralPoint, newLocation)
  266. if (dis > farthestDistance) {
  267. farthestDistance = dis
  268. farthestCoordinate = newLocation
  269. }
  270. }
  271. farthestDistance = farthestDistance / 1000 // 转为km
  272. // 根据坐标计算出合理的比例尺寸
  273. let surface = [500, 200, 100, 50, 20, 10, 5, 2, 1, 0.5, 0.2, 0.1, 0.05, 0.02]
  274. for (let i in surface) {
  275. if (farthestDistance > surface[i]) {
  276. let scale = 5 + Number(i)
  277. return scale
  278. }
  279. }
  280. return 16
  281. },
  282. routePost(data){
  283. console.log(data,'参数');
  284. routePost(data).then(res=>{
  285. console.log(res,'获取到的坐标范围');
  286. let addres=res.data.map(items=>{
  287. return {
  288. latitude: items[0],
  289. longitude: items[1],
  290. }
  291. })
  292. console.log(addres,'获取到的addres');
  293. this.graph(addres)
  294. })
  295. },
  296. updated() {
  297. console.log('更新完成了');
  298. },
  299. //绘制轨迹
  300. path(arrayData) {
  301. let polyline_obj = [{
  302. points: arrayData,
  303. arrowLine: true,
  304. // color: 'rgba(255, 0, 0, 1)',
  305. // width: 20,
  306. // borderColor: 'rgba(255, 0, 0, 1)',
  307. color: '#00CC6A',
  308. width: 22,
  309. borderColor: '#00CC6A',
  310. borderWidth: 20,
  311. dottedLine: true,
  312. }];
  313. this.polyline = polyline_obj;
  314. },
  315. //绘制矩形
  316. graph(arrayData) {
  317. console.log(arrayData, 'arrayData');
  318. let polygons_obj = [{
  319. points: arrayData,
  320. fillColor: '#376af64d',
  321. width: 5,
  322. strokeColor: '#9BC2FC', //描边颜色
  323. strokeWidth: 1, //描边宽度
  324. zIndex: 100, //层级
  325. }]
  326. this.polygons = polygons_obj;
  327. if(arrayData&&arrayData.length>0)
  328. {
  329. this.path(this.markers)
  330. //计算中心点
  331. this.latitude = eval(this.markers[0].latitude + '+' + this.markers[1].latitude) / 2
  332. this.longitude = eval(this.markers[0].longitude + '+' + this.markers[1].longitude) / 2
  333. this.map.latitude=this.latitude;
  334. this.map.longitude=this.longitude;
  335. //计算缩放比例
  336. this.scale = this.scaleDW(arrayData);
  337. }else{
  338. this.path(arrayData)
  339. }
  340. },
  341. // 规划路线的时候给起点一个marker,
  342. startingPoint(latitude, longitude, is) {
  343. console.log(latitude, longitude, '<-------我是开始坐标');
  344. this.startMarker = {
  345. id: 1,
  346. width: 110,
  347. height: 156,
  348. latitude: latitude,
  349. longitude: longitude,
  350. iconPath: "/static/images/path/star.png",
  351. anchor: {
  352. x: 0.5,
  353. y: 1,
  354. },
  355. };
  356. this.markers = [this.startMarker];
  357. this.map.longitude = longitude;
  358. this.map.latitude = latitude;
  359. if (!is) {
  360. this.$emit('getAddres', latitude + ',' + longitude)
  361. }
  362. },
  363. // 规划路线的时候给终点一个marker,
  364. endPoint(latitude, longitude, is) {
  365. let point = [{
  366. id: 2,
  367. width: 82,
  368. height: 95,
  369. latitude: latitude,
  370. longitude: longitude,
  371. iconPath: "/static/images/path/end.png",
  372. anchor: {
  373. x: 0.5,
  374. y: 1,
  375. },
  376. }, this.startMarker];
  377. this.markers = point;
  378. // console.log(this.markers, 'this.markers')
  379. this.map.longitude = longitude;
  380. this.map.latitude = latitude;
  381. if (!is) {
  382. this.$emit('getAddres', latitude + ',' + longitude)
  383. }
  384. },
  385. setMap(res) {
  386. this.longitude = res.longitude;
  387. this.latitude = res.latitude;
  388. this.markers[0].longitude = res.longitude;
  389. this.markers[0].latitude = res.latitude;
  390. }
  391. }
  392. }
  393. </script>
  394. <style>
  395. </style>