createApi.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. import _ from "lodash";
  2. import {getImages} from '@/plugins/images.js'
  3. import store from '@/store'
  4. //图标
  5. let cheliangtuli_tingche1 = getImages('/assetsMobile/svg/icon-cheliangtuli_tingche1.svg') //停留报警
  6. let cheliangtuli_baojing1 = getImages('/assetsMobile/svg/icon-cheliangtuli_baojing1.svg') //违规报警
  7. let origin = getImages('/assetsMobile/svg/origin.svg') //起点
  8. let destination = getImages('/assetsMobile/svg/destination.svg') //终点
  9. // 经过点类型 1.标注点 2.收集点 3.转运站 4.处理厂 5.公厕 6.投放点 7.垃圾桶 途径点不同类型图标
  10. let vehis1Icon = getImages('/assetsMobile/svg/icon-cheliangtuli_biaozhudian.svg')
  11. let vehis2Icon = getImages('/assetsMobile/svg/icon-cheliang_shoujidian.svg')
  12. let vehis3Icon = getImages('/assetsMobile/svg/icon-cheliangtuli_zhuanyunzhan.svg')
  13. let vehis4Icon = getImages('/assetsMobile/svg/icon-cheliang_chulichang.svg')
  14. let vehis5Icon = getImages('/assetsMobile/svg/icon-cheliang_toufangdian.svg')
  15. let vehis6Icon = getImages('/assetsMobile/svg/icon-cheliang_toufangdian.svg')
  16. let vehis7Icon = getImages('/assetsMobile/svg/icon-cheliangtuli_lajitong.svg')
  17. /**创建弧线*/
  18. export function createPulseLinkLayer(map) {
  19. // 创建 Loca 实例
  20. let loca = new Loca.Container({
  21. map: map
  22. });
  23. // 弧线
  24. var pulseLink = new Loca.PulseLinkLayer({
  25. // loca,
  26. zIndex: 10,
  27. opacity: 1,
  28. visible: true,
  29. zooms: [2, 22],
  30. depth: true,
  31. });
  32. var geo = new Loca.GeoJSONSource({
  33. url: 'https://a.amap.com/Loca/static/loca-v2/demos/mock_data/data-line-out.json',
  34. });
  35. pulseLink.setSource(geo);
  36. pulseLink.setStyle({
  37. unit: 'meter',
  38. dash: [40000, 0, 40000, 0],
  39. lineWidth: function () {
  40. return [10000, 1000];
  41. },
  42. height: function (index, feat) {
  43. return feat.distance / 3 + 10;
  44. },
  45. // altitude: 1000,
  46. smoothSteps: 30,
  47. speed: function (index, prop) {
  48. return 1000 + Math.random() * 200000;
  49. },
  50. flowLength: 100000,
  51. lineColors: function (index, feat) {
  52. return ['rgb(255,228,105)', 'rgb(255,164,105)', 'rgba(1, 34, 249,1)'];
  53. },
  54. maxHeightScale: 0.3, // 弧顶位置比例
  55. headColor: 'rgba(255, 255, 0, 1)',
  56. trailColor: 'rgba(255, 255,0,0)',
  57. });
  58. loca.add(pulseLink);
  59. loca.animate.start();
  60. }
  61. /**创建聚合点*/
  62. export function createMarkerCluster(map, data, callBack) {
  63. let { points } = data
  64. /**完全自定义点样式*/
  65. function _renderMarker(context) {
  66. // console.log(context,'context')
  67. // var content = `<img style="width: 28px;" src="${context.data[0].item.img}" />`;
  68. var content = `
  69. <div style="position: relative;">
  70. <img src="${context.data[0].item.img}"
  71. style="width: 28px; transform:rotate(${context.data[0].item.direction}deg)"/></br>
  72. <div style="
  73. border:solid 1px #00a0e9;
  74. background-color:#ffffff;
  75. position: absolute;
  76. padding: 2px;
  77. white-space: nowrap;
  78. left: 50%;
  79. top: 100%;
  80. font-size: 12px;
  81. transform: translate(-50%,0%);
  82. "
  83. >
  84. ${context.data[0].item.label}
  85. </div>
  86. </div>
  87. `;
  88. var offset = new AMap.Pixel(-14, -14);
  89. context.marker.setContent(content)
  90. context.marker.setOffset(offset)
  91. // context.marker.setAngle(context.data[0].item.direction)
  92. }
  93. let cluster = new AMap.MarkerCluster(map, points, {
  94. gridSize: 60, // 设置网格像素大小
  95. renderMarker: _renderMarker,
  96. clusterByZoomChange: false
  97. });
  98. let handleClickData = _.throttle((e) => {
  99. // console.log('testeeee',e)
  100. callBack(e)
  101. },500, {leading: true,trailing: false})
  102. cluster.on('click', (e) => {
  103. handleClickData(e)
  104. })
  105. return cluster
  106. }
  107. /**创建标注点*/
  108. let allmarkerArr = [];
  109. export function createMarker(map, data, callBack) {
  110. console.log('参', data)
  111. let iconMap = {
  112. 'vehStop': cheliangtuli_tingche1,
  113. 'veAlarm': cheliangtuli_baojing1,
  114. // 经过点类型 1.标注点 2.收集点 3.转运站 4.处理厂 5.公厕 6.投放点 7.垃圾桶
  115. 'vehis1': vehis1Icon,
  116. 'vehis2': vehis2Icon,
  117. 'vehis3': vehis3Icon,
  118. 'vehis4': vehis4Icon,
  119. 'vehis5': vehis5Icon,
  120. 'vehis6': vehis6Icon,
  121. 'vehis7': vehis7Icon,
  122. }
  123. let markerArr = data.list.map(e => {
  124. let icontype=data.type
  125. if(icontype==='vehis'){ //途径点
  126. icontype=`${icontype}${e.type}`
  127. e.cloneType = e.type
  128. }
  129. // 创建一个 Icon
  130. let startIcon = new AMap.Icon({
  131. // 图标尺寸
  132. size: new AMap.Size(31, 40),
  133. // 图标的取图地址
  134. image: iconMap[icontype],//'//a.amap.com/jsapi_demos/static/demo-center/icons/dir-marker.png',
  135. // 图标所用图片大小
  136. imageSize: new AMap.Size(31, 40),
  137. });
  138. let marker
  139. if (e.option) {
  140. marker = new AMap.Marker({
  141. ...e.option
  142. });
  143. } else {
  144. marker = new AMap.Marker({
  145. icon: startIcon,
  146. position: [e.lng02, e.lat02],
  147. offset: new AMap.Pixel(0, 6),
  148. anchor: 'bottom-center'
  149. });
  150. }
  151. marker.on('click', function (el) {
  152. console.log('点击', e)
  153. callBack('click', { ...e, type: data.type})
  154. })
  155. return marker
  156. })
  157. map.add(markerArr);
  158. // 实例方法
  159. allmarkerArr = allmarkerArr.concat(markerArr)
  160. const removeAllMarkers = () => {
  161. for (var i = 0; i < allmarkerArr.length; i++) {
  162. allmarkerArr[i].setMap(null); // 从地图上移除标记点
  163. }
  164. allmarkerArr = []; // 清空存储标记点的数组
  165. }
  166. return { removeAllMarkers };
  167. }
  168. /**创建轨迹回放 */
  169. let marker, originMarker, destinationMarker, polyline, passedPolyline, startAnimation, removeShowSpeed;
  170. const setCenterDebounce = _.throttle((map, e) => {
  171. var isVisible = map.getBounds().contains(e.target.getPosition());
  172. if (!isVisible) {
  173. map.setCenter(e.target.getPosition(), true)//自动聚焦
  174. }
  175. // map.setCenter(e.target.getPosition(), true)//自动聚焦
  176. }, 0);
  177. export function createMoveAlong(map, data, callBack) {
  178. console.log(marker, 'marker数据', data)
  179. let pointsData = data.points
  180. let points = pointsData.map(e => {
  181. return [Number(e.lng02), Number(e.lat02)]
  182. })
  183. let resetIdx;
  184. //清空已有实例
  185. if (marker) {
  186. marker.stopMove();
  187. marker.off('moving', markerMoving);
  188. marker.setMap(null);
  189. marker = null;
  190. }
  191. //清空起点和终点
  192. if (originMarker) {
  193. originMarker.setMap(null);
  194. originMarker = null
  195. }
  196. if (destinationMarker) {
  197. destinationMarker.setMap(null);
  198. destinationMarker = null
  199. }
  200. if (polyline) {
  201. polyline.setMap(null);
  202. polyline = null;
  203. }
  204. if (passedPolyline) {
  205. passedPolyline.setMap(null);
  206. passedPolyline = null;
  207. }
  208. if (removeShowSpeed) {
  209. removeShowSpeed();
  210. }
  211. if (pointsData.length <= 0) return; //无数据
  212. let duration = pointsData[0].duration //传入的速度
  213. let tabStyle = pointsData[0].tabStyle //传入的类型 人员车辆
  214. // 1、创建图标
  215. let icons
  216. if (store.state.pastRoute.mapImg) {
  217. icons = new AMap.Icon({
  218. image: store.state.pastRoute.mapImg, // 图标的图片地址
  219. size: new AMap.Size(28, 28), // 设置图标的大小,这里是宽度和高度均为48像素
  220. imageSize: new AMap.Size(28, 28), // 设置图标的大小,这里是宽度和高度均为48像素
  221. })
  222. } else {
  223. icons = "https://a.amap.com/jsapi_demos/static/demo-center-v2/car.png"
  224. }
  225. marker = new AMap.Marker({
  226. map: map,
  227. position: points[0], //points[0],
  228. icon: icons,
  229. zIndex: 18,
  230. offset: new AMap.Pixel(-13, -26),
  231. });
  232. //创建起点终点图标显示
  233. let originicons = new AMap.Icon({
  234. image: origin, // 图标的图片地址
  235. size: new AMap.Size(28, 28), // 设置图标的大小,这里是宽度和高度均为48像素
  236. imageSize: new AMap.Size(28, 28), // 设置图标的大小,这里是宽度和高度均为48像素
  237. })
  238. originMarker = new AMap.Marker({
  239. map: map,
  240. position: points[0], //points[0],
  241. icon: originicons,
  242. zIndex: 16,
  243. offset: new AMap.Pixel(-13, -26),
  244. });
  245. let destinationicons = new AMap.Icon({
  246. image: destination, // 图标的图片地址
  247. size: new AMap.Size(28, 28), // 设置图标的大小,这里是宽度和高度均为48像素
  248. imageSize: new AMap.Size(28, 28), // 设置图标的大小,这里是宽度和高度均为48像素
  249. })
  250. destinationMarker = new AMap.Marker({
  251. map: map,
  252. position: points[points.length - 1], //points[0],
  253. icon: destinationicons,
  254. zIndex: 16,
  255. offset: new AMap.Pixel(-13, -26),
  256. });
  257. // 2、创建路线
  258. polyline = new AMap.Polyline({
  259. map: map,
  260. path: points,
  261. zIndex: 10,
  262. showDir: true,
  263. strokeColor: "#28F", //线颜色
  264. // strokeOpacity: 1, //线透明度
  265. strokeWeight: 6, //线宽
  266. // strokeStyle: "solid" //线样式
  267. });
  268. // 3、创建行驶路线
  269. passedPolyline = new AMap.Polyline({
  270. zIndex: 13,
  271. map: map,
  272. strokeColor: "#AF5", //线颜色
  273. strokeWeight: 6, //线宽
  274. });
  275. // 4、事件监听
  276. marker.on('moving', markerMoving);
  277. function markerMoving(e) {
  278. console.log('更新轨迹', e)
  279. // 更新轨迹
  280. let passedPath = e.passedPath
  281. if (resetIdx) {
  282. e.index = e.index + resetIdx //拖拽
  283. passedPath = [...points.slice(0, resetIdx), ...e.passedPath] //points.slice(0,index+1).concat(e.passedPath)
  284. }
  285. if (tabStyle === 'vehicle') {
  286. marker.setAngle(pointsData[e.index + 1].direction) //手动设置方向
  287. }
  288. passedPolyline.setPath(passedPath);//聚焦
  289. setCenterDebounce(map, e)
  290. callBack('moving', e)
  291. }
  292. marker.on('click', function (e) {
  293. if (resetIdx) {
  294. e.resetIdx = resetIdx
  295. }
  296. callBack('click', e)
  297. })
  298. map.setFitView(marker,false,[60,60,160,60],10);
  299. // 开始
  300. startAnimation = function startAnimation() {
  301. let resetPoints;
  302. if (resetIdx) { //拖拽
  303. resetPoints = points.slice(resetIdx, points.length)
  304. if (resetPoints.length < 2) return; //moveAlong必须两点
  305. }
  306. marker.moveAlong(resetPoints || points, {
  307. // 每一段的时长
  308. duration: duration || 500,//可根据实际采集时间间隔设置
  309. // JSAPI2.0 是否延道路自动设置角度在 moveAlong 里设置
  310. autoRotation: false, //设置后setAngle无效BUG
  311. });
  312. };
  313. // 销毁事件
  314. const pauseAnimation = function () {
  315. marker.pauseMove();
  316. };
  317. const resumeAnimation = function () {
  318. marker.resumeMove();
  319. };
  320. const stopAnimation = function () {
  321. marker.stopMove();
  322. };
  323. const moveTo = function ({ lnglat, angle, index }) {
  324. stopAnimation()
  325. resetIdx = index
  326. marker.setPosition(lnglat)
  327. // console.log('设置方向', angle)
  328. if (tabStyle === 'vehicle') {
  329. marker.setAngle(angle)
  330. }
  331. passedPolyline.setPath(points.slice(0, index + 1));
  332. callBack('moveTo', { lnglat, angle, index })
  333. // marker.moveTo(lnglat) //卡顿BUG
  334. }
  335. const resetMoveAlong = function () {
  336. startAnimation()
  337. }
  338. // 显示速度
  339. let polylineArr = []
  340. const showSpeed = function () {
  341. for (var i = 0; i < points.length - 1; i++) {
  342. let itempolyline = new AMap.Polyline({
  343. path: [points[i], points[i + 1]],
  344. showDir: true,
  345. strokeColor: speedColor(pointsData[i].speed),
  346. strokeOpacity: 1,
  347. zIndex: 11,
  348. strokeWeight: 6,// 线宽
  349. map: map
  350. });
  351. polylineArr.push(itempolyline)
  352. }
  353. function speedColor(e) { //速度颜色
  354. let speed = Number(e)
  355. let itemColor = '#16B92A'
  356. if (speed > 60 && speed <= 80) {
  357. itemColor = '#0000FE'
  358. } else if (speed > 80 && speed <= 100) {
  359. itemColor = '#F98202'
  360. } else if (speed > 100) {
  361. itemColor = '#FE0100'
  362. }
  363. return itemColor
  364. }
  365. }
  366. removeShowSpeed = () => {
  367. for (var i = 0; i < polylineArr.length; i++) {
  368. polylineArr[i].setMap(null); // 从地图上移除标记点
  369. }
  370. polylineArr = []; // 清空存储标记点的数组
  371. }
  372. return {
  373. startAnimation,
  374. pauseAnimation,
  375. resumeAnimation,
  376. stopAnimation,
  377. moveTo,
  378. resetMoveAlong,
  379. showSpeed, //显示速度
  380. removeShowSpeed //关闭速度
  381. }
  382. }
  383. /**创建一条展示路线 */
  384. let PolylineShow,lineShoworiginMarker,lineShowdestinationMarker;
  385. export function createPolylineShow(map, data) {
  386. if (PolylineShow) {
  387. PolylineShow.setMap(null);
  388. PolylineShow = null;
  389. }
  390. if (lineShoworiginMarker) {
  391. lineShoworiginMarker.setMap(null);
  392. lineShoworiginMarker = null;
  393. }
  394. if (lineShowdestinationMarker) {
  395. lineShowdestinationMarker.setMap(null);
  396. lineShowdestinationMarker = null;
  397. }
  398. let { points } = data
  399. let pointsArr = points.map(e => {
  400. return [Number(e.lng02), Number(e.lat02)]
  401. })
  402. PolylineShow = new AMap.Polyline({
  403. map: map,
  404. path: pointsArr,
  405. strokeStyle: "solid",
  406. strokeColor: "#027AFF", //线颜色
  407. strokeWeight: 6, //线宽
  408. });
  409. //创建起点终点图标显示
  410. let originicons = new AMap.Icon({
  411. image: origin, // 图标的图片地址
  412. size: new AMap.Size(28, 28), // 设置图标的大小,这里是宽度和高度均为48像素
  413. imageSize: new AMap.Size(28, 28), // 设置图标的大小,这里是宽度和高度均为48像素
  414. })
  415. lineShoworiginMarker = new AMap.Marker({
  416. map: map,
  417. position: pointsArr[0], //points[0],
  418. icon: originicons,
  419. zIndex: 16,
  420. offset: new AMap.Pixel(-13, -26),
  421. });
  422. let destinationicons = new AMap.Icon({
  423. image: destination, // 图标的图片地址
  424. size: new AMap.Size(28, 28), // 设置图标的大小,这里是宽度和高度均为48像素
  425. imageSize: new AMap.Size(28, 28), // 设置图标的大小,这里是宽度和高度均为48像素
  426. })
  427. lineShowdestinationMarker = new AMap.Marker({
  428. map: map,
  429. position: pointsArr[pointsArr.length - 1], //points[0],
  430. icon: destinationicons,
  431. zIndex: 16,
  432. offset: new AMap.Pixel(-13, -26),
  433. });
  434. }