Amap.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <script setup lang="ts">
  2. import AMapLoader from "@amap/amap-jsapi-loader";
  3. import { reactive, getCurrentInstance, onBeforeMount, onUnmounted } from "vue";
  4. import { mapJson } from "/@/api/mock";
  5. import { deviceDetection } from "/@/utils/deviceDetection";
  6. import car from "/@/assets/car.png";
  7. export interface MapConfigureInter {
  8. on: Fn;
  9. destroy?: Fn;
  10. clearEvents?: Fn;
  11. addControl?: Fn;
  12. setCenter?: Fn;
  13. setZoom?: Fn;
  14. plugin?: Fn;
  15. }
  16. type resultType = {
  17. info: Array<undefined>;
  18. };
  19. let MarkerCluster;
  20. let map: MapConfigureInter;
  21. const instance = getCurrentInstance();
  22. const mapSet = reactive({
  23. loading: deviceDetection() ? false : true
  24. });
  25. // 地图创建完成(动画关闭)
  26. const complete = (): void => {
  27. if (map) {
  28. map.on("complete", () => {
  29. mapSet.loading = false;
  30. });
  31. }
  32. };
  33. onBeforeMount(() => {
  34. if (!instance) return;
  35. let { MapConfigure } = instance.appContext.config.globalProperties.$config;
  36. let { options } = MapConfigure;
  37. AMapLoader.load({
  38. key: MapConfigure.amapKey,
  39. version: "2.0",
  40. plugins: ["AMap.MarkerCluster"]
  41. })
  42. .then(AMap => {
  43. // 创建地图实例
  44. map = new AMap.Map(instance.refs.mapview, options);
  45. //地图中添加地图操作ToolBar插件
  46. map.plugin(["AMap.ToolBar", "AMap.MapType"], () => {
  47. map.addControl(new AMap.ToolBar());
  48. //地图类型切换
  49. map.addControl(
  50. new AMap.MapType({
  51. defaultType: 0
  52. })
  53. );
  54. });
  55. MarkerCluster = new AMap.MarkerCluster(map, [], {
  56. // 聚合网格像素大小
  57. gridSize: 80,
  58. maxZoom: 14,
  59. renderMarker(ctx) {
  60. let { marker, data } = ctx;
  61. if (Array.isArray(data) && data[0]) {
  62. var { driver, plateNumber, orientation } = data[0];
  63. var content = `<img style="transform: scale(1) rotate(${
  64. 360 - Number(orientation)
  65. }deg);" src='${car}' />`;
  66. marker.setContent(content);
  67. marker.setLabel({
  68. direction: "bottom",
  69. //设置文本标注偏移量
  70. offset: new AMap.Pixel(-4, 0),
  71. //设置文本标注内容
  72. content: `<div> ${plateNumber}(${driver})</div>`
  73. });
  74. marker.setOffset(new AMap.Pixel(-18, -10));
  75. marker.on("click", ({ lnglat }) => {
  76. map.setZoom(13); //设置地图层级
  77. map.setCenter(lnglat);
  78. });
  79. }
  80. }
  81. });
  82. // 获取模拟车辆信息
  83. mapJson()
  84. .then((res: resultType) => {
  85. let points: object = res.info.map((v: any) => {
  86. return {
  87. lnglat: [v.lng, v.lat],
  88. ...v
  89. };
  90. });
  91. if (MarkerCluster) MarkerCluster.setData(points);
  92. })
  93. .catch(err => {
  94. console.log("err:", err);
  95. });
  96. complete();
  97. })
  98. .catch(() => {
  99. mapSet.loading = false;
  100. throw "地图加载失败,请重新加载";
  101. });
  102. });
  103. onUnmounted(() => {
  104. if (map) {
  105. // 销毁地图实例
  106. map.destroy() && map.clearEvents("click");
  107. }
  108. });
  109. </script>
  110. <template>
  111. <div id="mapview" ref="mapview" v-loading="mapSet.loading"></div>
  112. </template>
  113. <style lang="scss" scoped>
  114. #mapview {
  115. height: calc(100vh - 86px);
  116. }
  117. :deep(.amap-marker-label) {
  118. border: none !important;
  119. }
  120. </style>