useAMap.ts 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. import AMapLoader from '@amap/amap-jsapi-loader';
  2. import { nanoid } from 'nanoid';
  3. import { deepClone, initDrag } from '@/utils';
  4. import { mergeGeoJsonPolygons, wgs_gcj_encrypts } from '@/utils/gisUtils';
  5. import carImg from '@/assets/images/car.png';
  6. export function useAMap(options) {
  7. let AMap, map, nowLayer, labelsLayer, scale, cluster;
  8. const markers = {
  9. point: []
  10. };
  11. let clickMarker = null;
  12. let addPoints = [];
  13. let layers = [];
  14. let defaultLayer;
  15. // 初始化事件
  16. const initMap = (options) => {
  17. window._AMapSecurityConfig = {
  18. securityJsCode: options.securityJsCode
  19. };
  20. AMapLoader.load({
  21. key: options.key, // 申请好的Web端开发者Key,首次调用 load 时必填
  22. version: !!options.version ? options.version : '2.0', // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
  23. plugins: options.plugins
  24. ? options.plugins
  25. : ['AMap.Scale', 'AMap.RangingTool', 'AMap.MouseTool', 'AMap.PolygonEditor', 'AMap.MarkerCluster', 'AMap.DistrictSearch', 'AMap.MoveAnimation', 'AMap.Driving', 'AMap.Geocoder', 'AMap.PlaceSearch', 'AMap.GeoJSON']
  26. }).then((res) => {
  27. AMap = res;
  28. defaultLayer = AMap.createDefaultLayer();
  29. map = new AMap.Map(options.el ? options.el : 'aMap', {
  30. WebGLParams: {
  31. preserveDrawingBuffer: true
  32. },
  33. layers: [defaultLayer],
  34. // 是否为3D地图模式
  35. // viewMode: '3D',
  36. pitch: options.pitch,
  37. // 初始化地图级别
  38. zoom: options.zoom ? options.zoom : 11,
  39. // 初始化地图中心点位置
  40. center: options.center,
  41. // 是否可拖拽
  42. dragEnable: options.dragEnable,
  43. // 是否允许通过鼠标滚轮来缩放
  44. scrollWheel: options.scrollWheel,
  45. maxZoom: options.maxZoom ? options.maxZoom : 20
  46. });
  47. // 初始化比例尺
  48. if (options.showScale) {
  49. scale = new AMap.Scale();
  50. map.addControl(scale);
  51. }
  52. if (typeof options.onLoadCompleted === 'function') {
  53. options.onLoadCompleted(AMap, map);
  54. }
  55. });
  56. };
  57. const getAMap = () => {
  58. return AMap;
  59. };
  60. const getMap = () => {
  61. return map;
  62. };
  63. const getScale = () => {
  64. return scale;
  65. };
  66. const initLayer = (type: string) => {
  67. if (defaultLayer) {
  68. map.removeLayer(defaultLayer);
  69. defaultLayer = null;
  70. }
  71. let keys = ['http://t0.tianditu.gov.cn/vec_w/wmts', 'http://t0.tianditu.gov.cn/cva_w/wmts'];
  72. if (type === 'satellite') {
  73. keys = ['http://t0.tianditu.gov.cn/img_w/wmts', 'http://t0.tianditu.gov.cn/cia_w/wmts'];
  74. }
  75. const layer = new AMap.TileLayer.WMTS({
  76. url: keys[0],
  77. blend: false,
  78. tileSize: 256,
  79. params: {
  80. Layer: 'img',
  81. Version: '1.0.0',
  82. Format: 'tiles',
  83. TileMatrixSet: 'w',
  84. STYLE: 'default',
  85. tk: 'a8df87f1695d224d2679aa805c1268d9'
  86. }
  87. });
  88. const layerMark = new AMap.TileLayer.WMTS({
  89. url: keys[1],
  90. blend: false,
  91. tileSize: 256,
  92. params: {
  93. Layer: type === 'satellite' ? 'cia' : 'cva',
  94. Version: '1.0.0',
  95. Format: 'tiles',
  96. TileMatrixSet: 'w',
  97. STYLE: 'default',
  98. tk: 'a8df87f1695d224d2679aa805c1268d9'
  99. }
  100. });
  101. return [layer, layerMark];
  102. };
  103. // 切换地图
  104. const switchMap = (type: string) => {
  105. layers.forEach((layer) => {
  106. map.removeLayer(layer);
  107. });
  108. layers = initLayer(type);
  109. layers.forEach((layer) => {
  110. layer.setMap(map);
  111. });
  112. };
  113. // 添加搜索的标记的
  114. const addSearchMarker = (item) => {
  115. map.setZoom(18);
  116. map.setCenter(item.lnglat);
  117. addMarker([item], true);
  118. clickMarker = item;
  119. options.onMarkerClick(item);
  120. };
  121. // 添加多个点
  122. const addMarker = (points, notClean?: boolean) => {
  123. if (!notClean) {
  124. clearMarker('point');
  125. }
  126. addPoints = points;
  127. const count = points.length;
  128. const _renderClusterMarker = function (context) {
  129. // 聚合中点个数
  130. const clusterCount = context.count;
  131. const div = document.createElement('div');
  132. div.style.backgroundColor = 'rgba(78,179,211,.5)';
  133. const size = Math.round(25 + Math.pow(clusterCount / count, 1 / 5) * 20);
  134. div.style.width = div.style.height = size + 'px';
  135. div.style.border = 'solid 1px rgba(78,179,211,1)';
  136. div.style.borderRadius = size / 2 + 'px';
  137. div.innerHTML = context.count;
  138. div.style.lineHeight = size + 'px';
  139. div.style.color = '#ffffff';
  140. div.style.fontSize = '12px';
  141. div.style.textAlign = 'center';
  142. context.marker.setOffset(new AMap.Pixel(-size / 2, -size / 2));
  143. context.marker.setContent(div);
  144. context.marker.on('click', (e) => {
  145. const bounds = e.target.getBounds();
  146. map.setZoomAndCenter(map.getZoom() + 1, bounds.getCenter());
  147. });
  148. };
  149. const _renderMarker = function (context) {
  150. const content =
  151. '<div style="display: flex;flex-direction: column;align-items: center;justify-content: center">' +
  152. '<div style="background: url(' +
  153. context.data[0].icon +
  154. ') no-repeat; width: ' +
  155. context.data[0].size[0] +
  156. 'px;height: ' +
  157. context.data[0].size[1] +
  158. 'px;cursor: pointer; background-size: cover"></div>' +
  159. // '<div style="font-size: 36px;white-space: nowrap">'+ context.data[0].name +'</div>' +
  160. '</div>';
  161. const offset = new AMap.Pixel(-9, -9);
  162. context.marker.setContent(content);
  163. context.marker.setOffset(offset);
  164. context.marker.setExtData(context.data[0]);
  165. context.marker.on('click', function (e) {
  166. const extData = e.target.getExtData();
  167. let index = 0;
  168. let index2 = 0;
  169. for (let i = 0; i < addPoints.length; i++) {
  170. if (addPoints[i].id === extData.id && addPoints[i].imageHover) {
  171. addPoints[i].icon = addPoints[i].imageHover;
  172. e.target.setContent(content);
  173. index++;
  174. } else if (!!clickMarker) {
  175. const extData2 = clickMarker.getExtData();
  176. if (addPoints[i].id === extData2.id) {
  177. addPoints[i].icon = addPoints[i].image;
  178. clickMarker.setContent(content);
  179. index2++;
  180. }
  181. }
  182. if ((!!clickMarker && index === 1 && index2 === 1) || (!clickMarker && index === 1)) {
  183. break;
  184. }
  185. }
  186. addMarker(addPoints);
  187. clickMarker = e.target;
  188. options.onMarkerClick(extData);
  189. });
  190. };
  191. cluster = new AMap.MarkerCluster(
  192. map, //地图实例
  193. points, //海量点数据,数据中需包含经纬度信息字段 lnglat
  194. {
  195. gridSize: 30, //数据聚合计算时网格的像素大小
  196. renderClusterMarker: _renderClusterMarker, //上述步骤的自定义聚合点样式
  197. renderMarker: _renderMarker //上述步骤的自定义非聚合点样式
  198. }
  199. );
  200. points.forEach((item) => {
  201. markers['point'].push(item);
  202. });
  203. };
  204. // 清除所有标加
  205. const clearMarker = (id) => {
  206. if (!cluster || !markers[id]) return;
  207. cluster.setMap(null);
  208. markers[id] = [];
  209. };
  210. const handleHover = (extData, dataType) => {
  211. map.setZoom(18);
  212. map.setCenter([extData.lng, extData.lat]);
  213. setTimeout(() => {
  214. let index = 0;
  215. let index2 = 0;
  216. let data = {};
  217. for (let i = 0; i < addPoints.length; i++) {
  218. if (addPoints[i].id === extData.id.toString() && addPoints[i].dataType === dataType) {
  219. addPoints[i].icon = addPoints[i].imageHover;
  220. index++;
  221. data = addPoints[i];
  222. } else if (!!clickMarker) {
  223. const extData2 = clickMarker.getExtData ? clickMarker.getExtData() : clickMarker;
  224. if (addPoints[i].id === extData2.id) {
  225. addPoints[i].icon = addPoints[i].image;
  226. index2++;
  227. }
  228. }
  229. if ((!!clickMarker && index === 1 && index2 === 1) || (!clickMarker && index === 1)) {
  230. break;
  231. }
  232. }
  233. addMarker(addPoints);
  234. options.onMarkerClick(data);
  235. }, 2000);
  236. };
  237. const getMarkers = () => {
  238. return markers;
  239. };
  240. // 显示信息框
  241. let infoWindow;
  242. const showInfo = (content, position, isCustom) => {
  243. hideInfo();
  244. // 实例化InfoWindow
  245. infoWindow = new AMap.InfoWindow({
  246. // 完全自定义
  247. isCustom: isCustom,
  248. autoMove: false,
  249. offset: new AMap.Pixel(0, -20) // 信息窗体的偏移量
  250. // 可以根据需要设置其他InfoWindow的属性
  251. });
  252. const lnglat = new AMap.LngLat(position[0], position[1]);
  253. // 打开InfoWindow,并设置其内容和位置
  254. infoWindow.setContent(content);
  255. infoWindow.open(map, lnglat);
  256. initDrag(infoWindow.dom);
  257. // 解决2.0版本无法滚动问题
  258. infoWindow.on('mouseover', () => map.setStatus({ zoomEnable: false }));
  259. infoWindow.on('mouseout', () => map.setStatus({ zoomEnable: true }));
  260. };
  261. const hideInfo = (e) => {
  262. map.setStatus({ zoomEnable: true });
  263. if (!!infoWindow) {
  264. infoWindow.close();
  265. if (!!clickMarker && e) {
  266. const extData = clickMarker.getExtData ? clickMarker.getExtData() : clickMarker;
  267. for (let i = 0; i < addPoints.length; i++) {
  268. if (addPoints[i].id === extData.id) {
  269. addPoints[i].icon = addPoints[i].image;
  270. clickMarker = null;
  271. addMarker(addPoints);
  272. break;
  273. }
  274. }
  275. }
  276. }
  277. };
  278. let maskPolygon;
  279. const creatMask = (options, name = '茂名市') => {
  280. new AMap.DistrictSearch({
  281. extensions: 'all',
  282. subdistrict: 0
  283. }).search(name, function (status, result) {
  284. // 外多边形坐标数组和内多边形坐标数组
  285. const outer = [
  286. new AMap.LngLat(-360, 90, true),
  287. new AMap.LngLat(-360, -90, true),
  288. new AMap.LngLat(360, -90, true),
  289. new AMap.LngLat(360, 90, true)
  290. ];
  291. options.forEach((option) => {
  292. const holes = result.districtList[0].boundaries;
  293. const pathArray = [outer];
  294. pathArray.push.apply(pathArray, holes);
  295. maskPolygon = new AMap.Polygon({
  296. pathL: pathArray,
  297. strokeColor: option.strokeColor ? option.strokeColor : '#268ab9',
  298. strokeOpacity: option.strokeOpacity ? option.strokeOpacity : 1,
  299. strokeWeight: option.strokeWeight ? option.strokeWeight : 1,
  300. fillColor: option.fillColor ? option.fillColor : '#10243b',
  301. fillOpacity: option.fillOpacity ? option.fillOpacity : 0.65
  302. });
  303. maskPolygon.setPath(pathArray);
  304. map.add(maskPolygon);
  305. });
  306. });
  307. };
  308. const removeMask = () => {
  309. if (!!maskPolygon) {
  310. map.remove(maskPolygon);
  311. maskPolygon = null;
  312. }
  313. };
  314. let maskPolygon2 = [];
  315. const creatMask2 = (data, option) => {
  316. if (maskPolygon2 && maskPolygon2.length > 0) {
  317. maskPolygon2.forEach((polygon) => {
  318. polygon.show();
  319. });
  320. } else {
  321. // data = convertCoordinates(data);
  322. // 遮罩部分
  323. // const outer = [
  324. // new AMap.LngLat(-180, 90, true),
  325. // new AMap.LngLat(-180, -90, true),
  326. // new AMap.LngLat(180, -90, true),
  327. // new AMap.LngLat(180, 90, true)
  328. // ];
  329. // const pathArray = [outer];
  330. // 合并区边界
  331. // const data2 = mergeGeoJsonPolygons(data);
  332. // data2.geometry.coordinates.forEach((coords) => {
  333. // pathArray.push(coords[0]);
  334. // });
  335. // const maskPolygon = new AMap.Polygon({
  336. // path: pathArray,
  337. // strokeColor: option.strokeColor ? option.strokeColor : '#268ab9',
  338. // strokeOpacity: 1,
  339. // strokeWeight: option.strokeWeight ? option.strokeWeight : 1,
  340. // fillColor: option.fillColor ? option.fillColor : '#10243b',
  341. // fillOpacity: option.fillOpacity ? option.fillOpacity : 0.65
  342. // });
  343. // maskPolygon2.push(maskPolygon);
  344. // map.add(maskPolygon);
  345. // 边界部分
  346. data.features.forEach((feature) => {
  347. if (feature.geometry.type === 'Polygon') {
  348. const polygonPath = feature.geometry.coordinates[0].map((coord) => {
  349. return [coord[0], coord[1]];
  350. });
  351. const polygon = new AMap.Polyline({
  352. path: polygonPath,
  353. strokeColor: option.strokeColor ? option.strokeColor : '#268ab9',
  354. strokeOpacity: option.strokeOpacity ? option.strokeOpacity : 1,
  355. strokeWeight: option.strokeWeight ? option.strokeWeight : 1,
  356. clickable: false
  357. });
  358. maskPolygon2.push(polygon);
  359. map.add(polygon);
  360. } else if (feature.geometry.type === 'MultiPolygon') {
  361. feature.geometry.coordinates.forEach((polygonCoords) => {
  362. const polygonPath = polygonCoords.map((ring) => {
  363. return ring.map((coord) => {
  364. return [coord[0], coord[1]];
  365. });
  366. });
  367. const outerPath = polygonPath[0];
  368. const innerPaths = polygonPath.slice(1);
  369. const polygon = new AMap.Polyline({
  370. path: outerPath,
  371. holes: innerPaths,
  372. strokeColor: option.strokeColor ? option.strokeColor : '#268ab9',
  373. strokeOpacity: option.strokeOpacity ? option.strokeOpacity : 1,
  374. strokeWeight: option.strokeWeight ? option.strokeWeight : 1,
  375. clickable: false
  376. });
  377. maskPolygon2.push(polygon);
  378. map.add(polygon);
  379. });
  380. }
  381. });
  382. }
  383. };
  384. const removeMask2 = (isHidden) => {
  385. if (maskPolygon2 && maskPolygon2.length > 0) {
  386. maskPolygon2.forEach((polygon) => {
  387. polygon.hide();
  388. });
  389. if (!isHidden) {
  390. maskPolygon2 = [];
  391. }
  392. }
  393. };
  394. let moveMarker, movePolyline, movePassedPolyline, timerId;
  395. const trackPlayback = (lineArr) => {
  396. if (timerId) {
  397. clearTimeout(timerId);
  398. }
  399. movePolyline?.remove();
  400. movePassedPolyline?.remove();
  401. moveMarker?.remove();
  402. let index = 0;
  403. const icon = new AMap.Icon({
  404. size: new AMap.Size(26, 52),
  405. image: carImg
  406. });
  407. moveMarker = new AMap.Marker({
  408. map: map,
  409. position: [116.478935, 39.997761],
  410. icon: icon,
  411. offset: new AMap.Pixel(-13, -26)
  412. });
  413. // 绘制轨迹
  414. movePolyline = new AMap.Polyline({
  415. map: map,
  416. path: lineArr,
  417. showDir: true,
  418. strokeColor: '#28F', //线颜色
  419. // strokeOpacity: 1, //线透明度
  420. strokeWeight: 6 //线宽
  421. // strokeStyle: "solid" //线样式
  422. });
  423. movePassedPolyline = new AMap.Polyline({
  424. map: map,
  425. strokeColor: '#AF5', //线颜色
  426. strokeWeight: 6 //线宽
  427. });
  428. moveMarker.on('moving', function (e) {
  429. movePassedPolyline.setPath(e.passedPath);
  430. map.setCenter(e.target.getPosition(), true);
  431. });
  432. moveMarker.on('moveend', function (e) {
  433. index++;
  434. if (index === lineArr.length - 1) {
  435. timerId = setTimeout(() => {
  436. movePolyline.remove();
  437. movePassedPolyline.remove();
  438. moveMarker.remove();
  439. }, 5000);
  440. }
  441. });
  442. moveMarker.moveAlong(lineArr, {
  443. // 每一段的时长
  444. duration: 1000, //可根据实际采集时间间隔设置
  445. // JSAPI2.0 是否延道路自动设置角度在 moveAlong 里设置
  446. autoRotation: true
  447. });
  448. };
  449. const drawData = (data) => {
  450. const res = [];
  451. data.forEach((item) => {
  452. let graphic;
  453. if (['rectangle', 'polygon', 'anyLine'].includes(item.type)) {
  454. graphic = new AMap.Polygon({
  455. path: item.path,
  456. strokeColor: item.strokeColor,
  457. strokeOpacity: item.strokeOpacity,
  458. strokeWeight: item.strokeWeight,
  459. fillColor: item.fillColor,
  460. fillOpacity: item.fillOpacity
  461. });
  462. graphic._opts.extData = {
  463. id: item.id
  464. };
  465. map.add(graphic);
  466. res.push(graphic);
  467. } else if (item.type === 'circle') {
  468. graphic = new AMap.Circle({
  469. center: item.center,
  470. radius: item.radius * 100000,
  471. strokeColor: item.strokeColor,
  472. strokeOpacity: item.strokeOpacity,
  473. strokeWeight: item.strokeWeight,
  474. fillColor: item.fillColor,
  475. fillOpacity: item.fillOpacity
  476. });
  477. graphic._opts.extData = {
  478. id: item.id
  479. };
  480. map.add(graphic);
  481. res.push(graphic);
  482. } else if (item.type === 'straightLine') {
  483. graphic = new AMap.Polyline({
  484. path: item.path,
  485. strokeColor: item.strokeColor,
  486. strokeOpacity: item.strokeOpacity,
  487. strokeWeight: item.strokeWeight,
  488. strokeStyle: item.strokeStyle
  489. });
  490. graphic._opts.extData = {
  491. id: item.id
  492. };
  493. map.add(graphic);
  494. res.push(graphic);
  495. } else if (item.type === 'text') {
  496. const { text } = addText(item);
  497. res.push(text);
  498. } else if (item.type === 'measureArea') {
  499. graphic = new AMap.Polygon({
  500. path: item.path,
  501. strokeColor: item.strokeColor,
  502. strokeOpacity: item.strokeOpacity,
  503. strokeWeight: item.strokeWeight,
  504. fillColor: item.fillColor,
  505. fillOpacity: item.fillOpacity
  506. });
  507. graphic._opts.extData = {
  508. id: item.id
  509. };
  510. map.add(graphic);
  511. // 计算区域面积
  512. const area = Math.round(AMap.GeometryUtil.ringArea(item.path));
  513. const text = new AMap.Text({
  514. position: item.path[item.path.length - 1],
  515. text: '区域面积' + area + '平方米',
  516. offset: new AMap.Pixel(-20, -20)
  517. });
  518. text._opts.extData = {
  519. id: item.id
  520. };
  521. data.area = area;
  522. map.add([graphic, text]);
  523. // res.push(text);
  524. } else if (item.type === 'marker') {
  525. // 创建标注点
  526. const marker = new AMap.Marker({
  527. position: new AMap.LngLat(item.longitude, item.latitude), // 标注点的位置
  528. icon: new AMap.Icon({
  529. size: item.size, //图标所处区域大小
  530. image: item.icon
  531. }),
  532. size: item.size
  533. });
  534. marker._opts.extData = {
  535. id: item.id
  536. };
  537. marker.setLabel({
  538. content: '<div>' + item.title + '</div>',
  539. direction: 'top'
  540. });
  541. map.add(marker);
  542. res.push(marker);
  543. }
  544. });
  545. return res;
  546. };
  547. const addText = (options) => {
  548. // 文本覆盖物的样式
  549. const textStyle = {
  550. fontSize: options.fontSize,
  551. color: options.fontColor,
  552. borderColor: 'transparent',
  553. backgroundColor: 'transparent',
  554. borderWidth: 0,
  555. cursor: 'pointer' // 鼠标悬停时显示指针
  556. };
  557. // 创建文本覆盖物
  558. const text = new AMap.Text({
  559. text: options.text, // 文本内容,可以根据需要自定义
  560. position: options.lnglat, // 文本位置(经纬度)
  561. style: textStyle, // 文本样式
  562. zIndex: 100, // 文本层级
  563. draggable: false // 是否可拖动(可选)
  564. });
  565. // 将文本覆盖物添加到地图
  566. map.add(text);
  567. const id = nanoid();
  568. text._opts.extData = {
  569. id: id
  570. };
  571. const data: any = deepClone(options);
  572. data.id = id;
  573. return { text, data };
  574. };
  575. const convertCoordinates = (geoJson) => {
  576. const features = geoJson.features.map((feature) => {
  577. const geometry = feature.geometry;
  578. let newGeometry;
  579. if (geometry.type === 'Point') {
  580. const [x, y] = geometry.coordinates;
  581. const obj = wgs_gcj_encrypts([{ lng: x, lat: y }])[0];
  582. newGeometry = { ...geometry, coordinates: [obj.lng, obj.lat] };
  583. } else if (geometry.type === 'MultiPoint') {
  584. const newCoordinates = geometry.coordinates.map((coords) => {
  585. const [x, y] = coords;
  586. const obj = wgs_gcj_encrypts([{ lng: x, lat: y }])[0];
  587. return [obj.lng, obj.lat];
  588. });
  589. newGeometry = { ...geometry, coordinates: newCoordinates };
  590. } else if (geometry.type === 'LineString') {
  591. const newCoordinates = geometry.coordinates.map((coords) => {
  592. const [x, y] = coords;
  593. const obj = wgs_gcj_encrypts([{ lng: x, lat: y }])[0];
  594. return [obj.lng, obj.lat];
  595. });
  596. newGeometry = { ...geometry, coordinates: newCoordinates };
  597. } else if (geometry.type === 'MultiLineString') {
  598. const newCoordinates = geometry.coordinates.map((line) =>
  599. line.map((coords) => {
  600. const [x, y] = coords;
  601. const obj = wgs_gcj_encrypts([{ lng: x, lat: y }])[0];
  602. return [obj.lng, obj.lat];
  603. })
  604. );
  605. newGeometry = { ...geometry, coordinates: newCoordinates };
  606. } else if (geometry.type === 'Polygon') {
  607. const newCoordinates = geometry.coordinates.map((polygon) =>
  608. polygon.map((coords) => {
  609. const [x, y] = coords;
  610. const obj = wgs_gcj_encrypts([{ lng: x, lat: y }])[0];
  611. return [obj.lng, obj.lat];
  612. })
  613. );
  614. newGeometry = { ...geometry, coordinates: newCoordinates };
  615. } else if (geometry.type === 'MultiPolygon') {
  616. const newCoordinates = geometry.coordinates.map((polygon) =>
  617. polygon.map((ring) =>
  618. ring.map((coords) => {
  619. const [x, y] = coords;
  620. const obj = wgs_gcj_encrypts([{ lng: x, lat: y }])[0];
  621. return [obj.lng, obj.lat];
  622. })
  623. )
  624. );
  625. newGeometry = { ...geometry, coordinates: newCoordinates };
  626. }
  627. return { ...feature, geometry: newGeometry };
  628. });
  629. return { ...geoJson, features };
  630. };
  631. onMounted(() => {
  632. initMap(options);
  633. });
  634. return {
  635. initMap,
  636. getAMap,
  637. getMap,
  638. switchMap,
  639. addMarker,
  640. addSearchMarker,
  641. clearMarker,
  642. getMarkers,
  643. getScale,
  644. showInfo,
  645. hideInfo,
  646. handleHover,
  647. creatMask,
  648. removeMask,
  649. creatMask2,
  650. removeMask2,
  651. trackPlayback,
  652. drawData
  653. };
  654. }