This commit is contained in:
WindowBird 2025-10-29 15:40:24 +08:00
parent 860cf08dbd
commit 5927d175cf
4 changed files with 81 additions and 22 deletions

View File

@ -24,15 +24,16 @@
</template>
<script setup>
import { ref, onMounted, onUnmounted } from 'vue'
import { ref, onMounted, onUnmounted, watch } from 'vue'
import { amapConfig, getAMapScriptUrl, validateKeys } from '~/config/amap'
//
const props = defineProps({
// [, ]
center: {
type: Array,
default: () => [117.397428, 31.90923] //
default: () => [120.258419, 27.11402]
},
//
zoom: {
@ -115,7 +116,7 @@ const initMap = async () => {
viewMode: props.viewMode, // 2D 3D
zoom: props.zoom, //
center: props.center, //
mapStyle: 'amap://styles/normal', //
features: ['bg', 'road', 'building', 'point'], //
showLabel: true, //
resizeEnable: true, //
@ -127,7 +128,7 @@ const initMap = async () => {
doubleClickZoom: true, //
scrollWheel: true, //
touchZoom: true, //
mapStyle: 'amap://styles/normal' //
})
//
@ -202,6 +203,30 @@ defineExpose({
}
})
// props
watch(() => props.center, (newCenter) => {
if (map.value && newCenter) {
console.log('地图中心点更新:', newCenter)
map.value.setCenter(newCenter)
}
}, { deep: true })
watch(() => props.zoom, (newZoom) => {
if (map.value && newZoom) {
console.log('地图缩放级别更新:', newZoom)
map.value.setZoom(newZoom)
}
})
watch(() => props.viewMode, (newViewMode) => {
if (map.value && newViewMode) {
console.log('地图视图模式更新:', newViewMode)
//
//
console.warn('视图模式需要在初始化时设置,当前版本不支持运行时切换')
}
})
//
onMounted(() => {
initMap()

View File

@ -85,7 +85,7 @@
<p class="text-gray-600 text-lg">多种方式联系我们我们随时为您服务</p>
</div>
<div class="grid lg:grid-cols-12 gap-8 items-start">
<div class="grid lg:grid-cols-12 gap-8 items-center">
<!-- 联系方式卡片 -->
<div class="col-span-12 lg:col-span-5">
<div class="bg-white rounded-xl shadow-lg p-8">
@ -110,11 +110,14 @@
<div class="col-span-12 lg:col-span-7">
<div class="bg-white rounded-xl shadow-lg p-6">
<h3 class="text-2xl font-semibold text-gray-800 mb-6 text-center">公司位置</h3>
<img
alt="公司位置地图 - 福建省福鼎市太姥山镇海埕路13号"
class="w-full rounded-lg shadow-md"
src="/img/map.png"
>
<AMapComponent
ref="mapRef"
:center="mapCenter"
:zoom="17"
height="342px"
@map-ready="onMapReady"
/>
<div class="mt-4 text-center">
<p class="text-gray-600">
<UIcon name="i-lucide-map-pin" class="w-5 h-5 inline mr-2 text-blue-500" />
@ -138,6 +141,40 @@
</template>
<script setup lang="ts">
// SEO
import AMapComponent from "~/components/AMapComponent.vue";
import {computed, ref} from 'vue'
const mapRef = ref(null)
const centerLng = ref(120.25834)
const centerLat = ref(27.114063)
const mapStatus = ref('加载中...')
const mapCenter = computed(() => [centerLng.value, centerLat.value])
const onMapReady = (mapInstance) => {
console.log('地图实例:', mapInstance)
mapStatus.value = '地图加载完成'
//
if (mapRef.value) {
mapRef.value.addMarker(mapCenter.value, {
title: '当前位置',
content: `
<div style="
background: #007bff;
color: white;
padding: 4px 8px;
border-radius: 4px;
font-size: 12px;
text-align: center;
transform: translate(-50%, -50%);
position: relative;
left: 50%;
top: 50%;
">创特物联</div>
`
})
}
}
useHead({
title: '小鹿骑行',
meta: [

View File

@ -1,6 +1,6 @@
<template>
<div class="map-demo-page">
<div class="page-header">
<div class="page-header mt-12">
<h1>高德地图组件演示</h1>
<p>基于您提供的HTML示例创建的Vue组件</p>
</div>
@ -112,7 +112,7 @@ const viewMode = ref('2D')
const mapStatus = ref('加载中...')
const lastClick = ref(null)
const currentCenter = ref(null)
const currentZoom = ref(11)
const currentZoom = ref(17)
//
const mapCenter = computed(() => [centerLng.value, centerLat.value])
@ -142,12 +142,12 @@ const quickLocations = [
const onMapReady = (mapInstance) => {
console.log('地图实例:', mapInstance)
mapStatus.value = '地图加载完成'
//
if (mapRef.value) {
mapRef.value.addMarker(mapCenter.value, {
title: '当前位置',
content: '<div style="background: #007bff; color: white; padding: 4px 8px; border-radius: 4px; font-size: 12px;">标记点</div>'
content: '<div style="background: #007bff; color: white; padding: 4px 8px; border-radius: 4px; font-size: 12px;">创特物联</div>'
})
}
}
@ -171,21 +171,18 @@ const onCenterChange = (center) => {
//
const updateCenter = () => {
if (mapRef.value) {
mapRef.value.setCenter(mapCenter.value)
}
// watch
//
console.log('更新中心点到:', mapCenter.value)
}
//
const goToLocation = (location) => {
console.log('快速定位到:', location.name, location.center)
centerLng.value = location.center[0]
centerLat.value = location.center[1]
zoomLevel.value = location.zoom
if (mapRef.value) {
mapRef.value.setCenter(location.center)
mapRef.value.setZoom(location.zoom)
}
// watch
}
</script>

BIN
public/img/6.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 213 KiB