feat(地图标记): 重构地图标记样式逻辑,支持图标和文字组合显示
重构地图标记的创建和样式管理逻辑,新增createMarkerContent方法统一处理标记内容生成。支持根据文字方向和选中状态动态调整样式,并优化标记点击时的样式切换逻辑。同时将Text标记改为Marker以支持自定义HTML内容。
Showing
2 changed files
with
70 additions
and
21 deletions
| ... | @@ -2,9 +2,9 @@ | ... | @@ -2,9 +2,9 @@ |
| 2 | VITE_PORT = 8006 | 2 | VITE_PORT = 8006 |
| 3 | 3 | ||
| 4 | # 反向代理服务器地址 | 4 | # 反向代理服务器地址 |
| 5 | -# VITE_PROXY_TARGET = https://oa-dev.onwall.cn | 5 | +VITE_PROXY_TARGET = https://oa-dev.onwall.cn |
| 6 | # VITE_PROXY_TARGET = https://bm.jiqun.com | 6 | # VITE_PROXY_TARGET = https://bm.jiqun.com |
| 7 | -VITE_PROXY_TARGET = https://oa.onwall.cn | 7 | +# VITE_PROXY_TARGET = https://oa.onwall.cn |
| 8 | 8 | ||
| 9 | # API请求前缀 | 9 | # API请求前缀 |
| 10 | VITE_PROXY_PREFIX = /srv/ | 10 | VITE_PROXY_PREFIX = /srv/ | ... | ... |
| ... | @@ -414,6 +414,42 @@ export default { | ... | @@ -414,6 +414,42 @@ export default { |
| 414 | 414 | ||
| 415 | methods: { | 415 | methods: { |
| 416 | ...mapActions(mainStore, ['changeAudio', 'changeAudioSrc', 'changeAudioStatus']), | 416 | ...mapActions(mainStore, ['changeAudio', 'changeAudioSrc', 'changeAudioStatus']), |
| 417 | + /** | ||
| 418 | + * 创建标记点的HTML内容,包含图标和文字 | ||
| 419 | + * @param {Object} entityInfo - 实体信息 | ||
| 420 | + * @param {String} textDirection - 文字方向 ('vertical' 或 'horizontal') | ||
| 421 | + * @param {Boolean} isSelected - 是否选中状态 | ||
| 422 | + * @returns {String} HTML字符串 | ||
| 423 | + */ | ||
| 424 | + createMarkerContent(entityInfo, textDirection, isSelected) { | ||
| 425 | + const iconUrl = entityInfo.icon || ''; | ||
| 426 | + const name = entityInfo.name || ''; | ||
| 427 | + | ||
| 428 | + // 根据选中状态和文字方向选择样式 | ||
| 429 | + let textStyle, containerStyle; | ||
| 430 | + if (textDirection === 'vertical') { | ||
| 431 | + textStyle = isSelected ? this.markerStyle2 : this.markerStyle1; | ||
| 432 | + containerStyle = 'flex-direction: column; align-items: center;'; | ||
| 433 | + } else { | ||
| 434 | + textStyle = isSelected ? this.markerStyle2_horizontal : this.markerStyle1_horizontal; | ||
| 435 | + containerStyle = 'flex-direction: row; align-items: center;'; | ||
| 436 | + } | ||
| 437 | + | ||
| 438 | + // 将样式对象转换为CSS字符串 | ||
| 439 | + const textStyleStr = Object.entries(textStyle) | ||
| 440 | + .map(([key, value]) => `${key}: ${value}`) | ||
| 441 | + .join('; '); | ||
| 442 | + | ||
| 443 | + // 创建HTML内容 | ||
| 444 | + const html = ` | ||
| 445 | + <div style="display: flex; ${containerStyle} cursor: pointer;"> | ||
| 446 | + ${iconUrl ? `<img src="${iconUrl}" style="width: 20px; height: 20px; margin: ${textDirection === 'vertical' ? '0 0 4px 0' : '0 4px 0 0'};" />` : ''} | ||
| 447 | + <div style="${textStyleStr}">${name}</div> | ||
| 448 | + </div> | ||
| 449 | + `; | ||
| 450 | + | ||
| 451 | + return html; | ||
| 452 | + }, | ||
| 417 | initMap() { | 453 | initMap() { |
| 418 | // 初始化地图 | 454 | // 初始化地图 |
| 419 | this.map = new AMap.Map('container', { | 455 | this.map = new AMap.Map('container', { |
| ... | @@ -462,15 +498,15 @@ export default { | ... | @@ -462,15 +498,15 @@ export default { |
| 462 | marker_icon = entity_info[i].icon; | 498 | marker_icon = entity_info[i].icon; |
| 463 | } | 499 | } |
| 464 | let text_direction = entity_info[i]?.writing_mode === 'vertical' ? 'vertical' : 'horizontal'; | 500 | let text_direction = entity_info[i]?.writing_mode === 'vertical' ? 'vertical' : 'horizontal'; |
| 465 | - let textMarker = new AMap.Text({ | 501 | + |
| 502 | + // 创建自定义HTML内容,包含图标和文字 | ||
| 503 | + const markerContent = this.createMarkerContent(entity_info[i], text_direction, false); | ||
| 504 | + | ||
| 505 | + let textMarker = new AMap.Marker({ | ||
| 466 | zooms: this.data_zooms, // 点标记显示的层级范围,使用地图配置的缩放范围 | 506 | zooms: this.data_zooms, // 点标记显示的层级范围,使用地图配置的缩放范围 |
| 467 | - text: entity_info[i].name, //标记显示的文本内容 | 507 | + content: markerContent, // 自定义HTML内容 |
| 468 | - anchor: "center", //设置文本标记锚点位置 | 508 | + anchor: "center", // 设置标记锚点位置 |
| 469 | - // draggable: true, //是否可拖拽 | 509 | + position: entity_info[i].position, // 点标记在地图上显示的位置 |
| 470 | - // cursor: "pointer", //指定鼠标悬停时的鼠标样式。 | ||
| 471 | - // angle: 10, //点标记的旋转角度 | ||
| 472 | - style: text_direction === 'vertical' ? this.markerStyle1 : this.markerStyle1_horizontal, | ||
| 473 | - position: entity_info[i].position, //点标记在地图上显示的位置 | ||
| 474 | }); | 510 | }); |
| 475 | textMarker.setMap(this.map); //将文本标记设置到地图上 | 511 | textMarker.setMap(this.map); //将文本标记设置到地图上 |
| 476 | this.markerSum.push(textMarker); | 512 | this.markerSum.push(textMarker); |
| ... | @@ -479,15 +515,21 @@ export default { | ... | @@ -479,15 +515,21 @@ export default { |
| 479 | } | 515 | } |
| 480 | // 绑定景点的点击事件 - 文字出现才能触发 | 516 | // 绑定景点的点击事件 - 文字出现才能触发 |
| 481 | var clickListener1 = textMarker.on('click', async (e) => { | 517 | var clickListener1 = textMarker.on('click', async (e) => { |
| 482 | - // 还原样式 | 518 | + // 还原样式 - 将其他标记设为未选中状态 |
| 483 | - this.markerSum.forEach(item => { | 519 | + this.markerSum.forEach((item, index) => { |
| 484 | - if (e.target.hS !== item.hS) { | 520 | + if (item !== textMarker) { |
| 485 | - // 修改文本的样式 | 521 | + const itemInfo = entity_info[index] || entity_info.find(info => info.position[0] === item.getPosition().lng && info.position[1] === item.getPosition().lat); |
| 486 | - item.setStyle(item._x['writing-mode'] === 'vertical-rl' ? this.markerStyle2 : this.markerStyle2_horizontal); | 522 | + if (itemInfo) { |
| 523 | + const itemDirection = itemInfo?.writing_mode === 'vertical' ? 'vertical' : 'horizontal'; | ||
| 524 | + const newContent = this.createMarkerContent(itemInfo, itemDirection, false); | ||
| 525 | + item.setContent(newContent); | ||
| 487 | } | 526 | } |
| 488 | - }) | 527 | + } |
| 489 | - // 修改文本的样式 | 528 | + }); |
| 490 | - e.target.setStyle(text_direction === 'vertical' ? this.markerStyle1 : this.markerStyle1_horizontal); | 529 | + |
| 530 | + // 设置当前标记为选中状态 | ||
| 531 | + const newContent = this.createMarkerContent(entity_info[i], text_direction, true); | ||
| 532 | + textMarker.setContent(newContent); | ||
| 491 | 533 | ||
| 492 | // 修改文本内容 | 534 | // 修改文本内容 |
| 493 | // textMarker.setText('样式已修改'); | 535 | // textMarker.setText('样式已修改'); |
| ... | @@ -941,9 +983,16 @@ export default { | ... | @@ -941,9 +983,16 @@ export default { |
| 941 | this.$refs.pageInfo.outerStopAudio(); | 983 | this.$refs.pageInfo.outerStopAudio(); |
| 942 | }, | 984 | }, |
| 943 | resetMarkStyle () { | 985 | resetMarkStyle () { |
| 944 | - this.markerSum.forEach(item => { | 986 | + // 重置所有标记为未选中状态 |
| 945 | - item.setStyle(item._x['writing-mode'] === 'vertical-rl' ? this.markerStyle1 : this.markerStyle1_horizontal); | 987 | + this.markerSum.forEach((item, index) => { |
| 946 | - }) | 988 | + // 获取对应的实体信息 |
| 989 | + const entityInfo = this.navList[index]; | ||
| 990 | + if (entityInfo) { | ||
| 991 | + const textDirection = entityInfo?.writing_mode === 'vertical' ? 'vertical' : 'horizontal'; | ||
| 992 | + const newContent = this.createMarkerContent(entityInfo, textDirection, false); | ||
| 993 | + item.setContent(newContent); | ||
| 994 | + } | ||
| 995 | + }); | ||
| 947 | }, | 996 | }, |
| 948 | onCloseFloat () { | 997 | onCloseFloat () { |
| 949 | this.info_height = 0; | 998 | this.info_height = 0; | ... | ... |
-
Please register or login to post a comment