AMapContainer.vue 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <template>
  2. <div>
  3. <div :id="mapId" class="container"></div>
  4. <el-dialog v-model="mediaModal" :title="modalTitle" destroy-on-close>
  5. <el-image
  6. v-if="modalType == 1"
  7. style="height: 60vh; display: flex"
  8. fit="contain"
  9. :src="currentUrl"
  10. :preview-src-list="modalPreview"
  11. ></el-image>
  12. <video
  13. v-else
  14. autoplay
  15. controls
  16. style="width: 100%; height: 60vh"
  17. :src="currentUrl"
  18. ></video>
  19. </el-dialog>
  20. </div>
  21. </template>
  22. <script setup>
  23. import { onMounted, onUnmounted, ref, nextTick } from "vue";
  24. import AMapLoader from "@amap/amap-jsapi-loader";
  25. import router from "../router";
  26. import { useRoute } from "vue-router";
  27. const route = useRoute();
  28. const props = defineProps({
  29. longitude: {
  30. type: Number,
  31. default: 120.498409,
  32. },
  33. latitude: {
  34. type: Number,
  35. default: 32.039665,
  36. },
  37. ships: {
  38. type: Array,
  39. default: [],
  40. },
  41. medias: {
  42. type: Array,
  43. default: [],
  44. },
  45. coordinates: {
  46. type: Array,
  47. default: [],
  48. },
  49. showLabel: {
  50. type: Boolean,
  51. default: false,
  52. },
  53. handleAble: {
  54. type: Boolean,
  55. default: false,
  56. },
  57. loadPorts: {
  58. type: Array,
  59. default: [],
  60. },
  61. dischargePorts: {
  62. type: Array,
  63. default: [],
  64. },
  65. mapId: {
  66. type: String,
  67. default: "container",
  68. },
  69. zoomEnable: {
  70. type: Boolean,
  71. default: true,
  72. },
  73. dragEnable: {
  74. type: Boolean,
  75. default: true,
  76. },
  77. ToolBar: {
  78. type: Boolean,
  79. default: true,
  80. },
  81. HawkEye: {
  82. type: Boolean,
  83. default: true,
  84. },
  85. });
  86. const mapId = ref(props.mapId);
  87. const map = ref({});
  88. function initMap() {
  89. AMapLoader.load({
  90. key: "0b84075e96d01623f704867a601139bb", // 申请好的Web端开发者Key,首次调用 load 时必填
  91. version: "2.0", // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15需要使用的的插件列表,如比例尺'AMap.Scale',支持添加多个如:['...','...']
  92. })
  93. .then((AMap) => {
  94. map.value = new AMap.Map(props.mapId, {
  95. zoom: 12, // 初始化地图级别
  96. center: [props.longitude, props.latitude], // 初始化地图中心点位置
  97. mapStyle: "amap://styles/f48d96805f5fa7f5aada657c5ee37017",
  98. zoomEnable: props.zoomEnable,
  99. dragEnable: props.dragEnable,
  100. });
  101. if (props.ToolBar || props.HawkEye) {
  102. map.value.plugin(["AMap.ToolBar", "AMap.HawkEye"], function () {
  103. if (props.ToolBar) {
  104. let ToolBar = new AMap.ToolBar({
  105. position: {
  106. top: "40px",
  107. right: "40px",
  108. },
  109. });
  110. map.value.addControl(ToolBar);
  111. }
  112. if (props.HawkEye) {
  113. let HawkEye = new AMap.HawkEye({
  114. opened: false,
  115. });
  116. map.value.addControl(HawkEye);
  117. }
  118. });
  119. }
  120. })
  121. .catch((e) => {
  122. console.log(e);
  123. });
  124. }
  125. function generateMarker({
  126. id = 0,
  127. longitude = 120.498409,
  128. latitude = 32.039665,
  129. offsetX = -15,
  130. offsetY = -25,
  131. zIndex = 1,
  132. label = "",
  133. handleType = "",
  134. viewUrl = "",
  135. content = `<div>
  136. <img src='https://hhd-pat-1255802371.cos.ap-shanghai.myqcloud.com/frontend/ship-red-icon.png' style='display:block;width:20px;height:20px;margin:6px auto'/
  137. </div>`,
  138. }) {
  139. let marker = new AMap.Marker({
  140. content,
  141. zIndex,
  142. position: new AMap.LngLat(longitude, latitude),
  143. offset: new AMap.Pixel(offsetX, offsetY),
  144. });
  145. if (label) {
  146. marker.setLabel({
  147. direction: "top",
  148. offset: new AMap.Pixel(0, 0), //设置文本标注偏移量
  149. content: label, //设置文本标注内容
  150. style: "",
  151. });
  152. }
  153. if (handleType === "route") {
  154. marker.on("click", () => {
  155. goTo("/voyage/voyageDetail", {
  156. id,
  157. });
  158. });
  159. }
  160. if (handleType === "media") {
  161. marker.on("click", () => {
  162. openMediaModal(viewUrl, 1, "航次图片");
  163. });
  164. }
  165. return marker;
  166. }
  167. //ship-orange-icon
  168. function generateMarkers(points, type) {
  169. let markers = [];
  170. for (let i of points) {
  171. let config = {
  172. longitude: i.longitude,
  173. latitude: i.latitude,
  174. };
  175. switch (type) {
  176. case "ships": {
  177. config.label = i.shipName;
  178. config.id = i.voyageId;
  179. config.handleType = "route";
  180. if (route.path === "/index") {
  181. config.content = `<div>
  182. <img src='https://frontend-1255802371.cos.ap-shanghai.myqcloud.com/ship-orange-icon.png' style='display:block;width:30px;height:30px;margin:6px auto'/
  183. </div>`;
  184. }
  185. break;
  186. }
  187. case "loadPorts": {
  188. config.content = `<div>
  189. <img src='https://hhd-pat-1255802371.cos.ap-shanghai.myqcloud.com/frontend/port-green.png' style='display:block;width:30px;height:30px;margin:6px auto'/
  190. </div>`;
  191. config.label = "装货港";
  192. break;
  193. }
  194. case "dischargePorts": {
  195. config.content = `<div>
  196. <img src='https://hhd-pat-1255802371.cos.ap-shanghai.myqcloud.com/frontend/port-red.png' style='display:block;width:30px;height:30px;margin:6px auto'/
  197. </div>`;
  198. config.label = "卸货港";
  199. break;
  200. }
  201. case "medias": {
  202. config.content = `<div style='width:160px'>
  203. ${
  204. i.audit == 1
  205. ? `<img id='img${i.id}' style='width:160px;height:160px;object-fit:contain;' src='${i.viewUrl}'/>`
  206. : ``
  207. }
  208. <img src='https://hhd-pat-1255802371.cos.ap-shanghai.myqcloud.com/frontend/ship-red-icon.png' style='display:block;width:20px;height:20px;margin:6px auto'/
  209. </div>`;
  210. config.offsetX = -80;
  211. config.offsetY = i.audit == 1 ? -185 : -20;
  212. config.zIndex = 15;
  213. config.handleType = "media";
  214. config.viewUrl = i.viewUrl;
  215. break;
  216. }
  217. case "coordinates": {
  218. config.content = `<div>
  219. <img src='https://frontend-1255802371.cos.ap-shanghai.myqcloud.com/green-circle.png' style='display:block;width:20px;height:20px;margin:6px auto'/
  220. </div>`;
  221. config.offsetX = -10;
  222. config.offsetY = -17;
  223. break;
  224. }
  225. }
  226. markers.push(generateMarker(config));
  227. }
  228. return markers;
  229. }
  230. async function drawMarkers() {
  231. await nextTick();
  232. let markers = [
  233. ...generateMarkers(props.ships, "ships"),
  234. ...generateMarkers(props.loadPorts, "loadPorts"),
  235. ...generateMarkers(props.dischargePorts, "dischargePorts"),
  236. ...generateMarkers(props.medias, "medias"),
  237. ...generateMarkers(props.coordinates, "coordinates"),
  238. ];
  239. let overlayGroups = new AMap.OverlayGroup(markers);
  240. map.value.add(overlayGroups);
  241. setFitView(markers);
  242. }
  243. function setFitView(markers = [], zoom = 9) {
  244. map.value.setFitView(markers, true, [150, 50, 100, 100], zoom);
  245. }
  246. function clearMap() {
  247. map.value.clearMap();
  248. }
  249. function goTo(path = "/voyage/voyageDetail", query = {}) {
  250. router.push({
  251. path,
  252. query,
  253. });
  254. }
  255. let currentUrl = ref("");
  256. let mediaModal = ref(false);
  257. let modalType = ref(1);
  258. let modalTitle = ref("");
  259. let modalPreview = ref([]);
  260. function openMediaModal(url, type, title, item = {}) {
  261. modalPreview.value = [url];
  262. modalTitle.value = title;
  263. modalType.value = type;
  264. currentUrl.value = url;
  265. mediaModal.value = true;
  266. }
  267. onMounted(() => {
  268. initMap();
  269. });
  270. defineExpose({
  271. clearMap,
  272. drawMarkers,
  273. initMap,
  274. });
  275. onUnmounted(() => {
  276. map.value.destroy();
  277. });
  278. </script>
  279. <style scoped>
  280. #container {
  281. width: 100%;
  282. }
  283. .container {
  284. width: 100%;
  285. height: 100%;
  286. }
  287. </style>