useAMap.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import AMapLoader from '@amap/amap-jsapi-loader';
  2. export function useAMap(options) {
  3. let AMap, map, nowLayer, labelsLayer, scale, cluster;
  4. const markers = {
  5. point: []
  6. };
  7. // 初始化事件
  8. const initMap = (options) => {
  9. AMapLoader.load({
  10. key: options.key, // 申请好的Web端开发者Key,首次调用 load 时必填
  11. version: !!options.version ? options.version : '2.0', // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
  12. plugins: options.plugins ? options.plugins : ['AMap.Scale', 'AMap.RangingTool', 'AMap.MouseTool', 'AMap.PolygonEditor', 'AMap.MarkerCluster']
  13. }).then((res) => {
  14. AMap = res;
  15. map = new AMap.Map('aMap', {
  16. // 是否为3D地图模式
  17. viewMode: '3D',
  18. pitch: options.pitch,
  19. // 初始化地图级别
  20. zoom: options.zoom ? options.zoom : 11,
  21. // 初始化地图中心点位置
  22. center: options.center,
  23. // 是否可拖拽
  24. dragEnable: options.dragEnable,
  25. // 是否允许通过鼠标滚轮来缩放
  26. scrollWheel: options.scrollWheel
  27. });
  28. // 初始化比例尺
  29. if (options.showScale) {
  30. scale = new AMap.Scale();
  31. map.addControl(scale);
  32. }
  33. if (typeof options.onLoadCompleted === 'function') {
  34. options.onLoadCompleted(AMap, map);
  35. }
  36. });
  37. };
  38. const getAMap = () => {
  39. return AMap;
  40. };
  41. const getMap = () => {
  42. return map;
  43. };
  44. const getScale = () => {
  45. return scale;
  46. };
  47. // 切换地图
  48. const switchMap = (type: string) => {
  49. if (type === 'vectorgraph') {
  50. map.removeLayer(nowLayer);
  51. } else if (type === 'satellite') {
  52. const satellite = new AMap.TileLayer.Satellite();
  53. map.addLayer(satellite);
  54. nowLayer = satellite;
  55. }
  56. };
  57. // 添加搜索的标记的
  58. const addSearchMarker = (item) => {
  59. map.setCenter([item.longitude, item.latitude]);
  60. addMarker(item, true);
  61. };
  62. const addMarker = (points, isSearchItem?: boolean) => {
  63. clearMarker('point');
  64. const count = points.length;
  65. const _renderClusterMarker = function (context) {
  66. console.log(context);
  67. console.log('21312', context.clusterData);
  68. // 聚合中点个数
  69. const clusterCount = context.count;
  70. const div = document.createElement('div');
  71. div.style.backgroundColor = 'rgba(78,179,211,.5)';
  72. const size = Math.round(25 + Math.pow(clusterCount / count, 1 / 5) * 20);
  73. div.style.width = div.style.height = size + 'px';
  74. div.style.border = 'solid 1px rgba(78,179,211,1)';
  75. div.style.borderRadius = size / 2 + 'px';
  76. div.innerHTML = context.count;
  77. div.style.lineHeight = size + 'px';
  78. div.style.color = '#ffffff';
  79. div.style.fontSize = '12px';
  80. div.style.textAlign = 'center';
  81. context.marker.setOffset(new AMap.Pixel(-size / 2, -size / 2));
  82. context.marker.setContent(div);
  83. context.marker.on('click', (e) => {
  84. debugger;
  85. });
  86. };
  87. const _renderMarker = function (context) {
  88. const content =
  89. '<div style="display: flex;flex-direction: column;align-items: center;justify-content: center">' +
  90. '<div style="background: url(' +
  91. context.data[0].image +
  92. ') no-repeat; width: ' +
  93. context.data[0].size[0] +
  94. 'px;height: ' +
  95. context.data[0].size[1] +
  96. 'px;cursor: pointer; background-size: cover"></div>' +
  97. // '<div style="font-size: 36px;white-space: nowrap">'+ context.data[0].name +'</div>' +
  98. '</div>';
  99. const offset = new AMap.Pixel(-9, -9);
  100. context.marker.setContent(content);
  101. context.marker.setOffset(offset);
  102. context.marker.setExtData(context.data[0]);
  103. context.marker.on('click', function (e) {
  104. options.onMarkerClick(e.target.getExtData());
  105. });
  106. };
  107. cluster = new AMap.MarkerCluster(
  108. map, //地图实例
  109. points, //海量点数据,数据中需包含经纬度信息字段 lnglat
  110. {
  111. gridSize: 20, //数据聚合计算时网格的像素大小
  112. renderClusterMarker: _renderClusterMarker, //上述步骤的自定义聚合点样式
  113. renderMarker: _renderMarker //上述步骤的自定义非聚合点样式
  114. }
  115. );
  116. points.forEach((item) => {
  117. markers['point'].push(item);
  118. });
  119. };
  120. // 清除所有标加
  121. const clearMarker = (id) => {
  122. if (!cluster || !markers[id]) return;
  123. cluster.setMap(null);
  124. markers[id] = [];
  125. };
  126. const getMarkers = () => {
  127. return markers;
  128. };
  129. // 显示信息框
  130. const showInfo = (content, position) => {
  131. // 实例化InfoWindow
  132. const infoWindow = new AMap.InfoWindow({
  133. // 完全自定义
  134. // isCustom: true,
  135. offset: new AMap.Pixel(0, -20) // 信息窗体的偏移量
  136. // 可以根据需要设置其他InfoWindow的属性
  137. });
  138. const lnglat = new AMap.LngLat(position[0], position[1]);
  139. // 打开InfoWindow,并设置其内容和位置
  140. infoWindow.setContent(content);
  141. infoWindow.open(map, lnglat);
  142. };
  143. onMounted(() => {
  144. initMap(options);
  145. });
  146. return {
  147. getAMap,
  148. getMap,
  149. switchMap,
  150. addMarker,
  151. addSearchMarker,
  152. clearMarker,
  153. getMarkers,
  154. getScale,
  155. showInfo
  156. };
  157. }