杨磊
3 天以前 c0f55c89a32df439aa2c82d9ca88c4cee8c5d86d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<template>
  <div class="map-container">
    <!-- 地图容器 -->
    <div id="map-container" ref="mapContainer"></div>
  </div>
</template>
 
<script setup>
import { ref, onMounted } from 'vue'
import AMapLoader from '@amap/amap-jsapi-loader'
 
// 配置安全密钥(推荐)
window._AMapSecurityConfig = {
  securityJsCode: '4c6cee842f5412a45a781aeaa76e12cc', // 高德控制台获取
}
 
const mapContainer = ref(null)
const mapInstance = ref(null)
const marker = ref(null)
 
// 示例地点数据
const location = ref({
  name: '天安门广场',
  address: '北京市东城区长安街',
  position: [116.413823, 39.912052],
})
 
// 初始化地图
onMounted(() => {
  AMapLoader.load({
    key: '4c6cee842f5412a45a781aeaa76e12cc', // 替换为你的Key
    version: '2.0', // SDK版本
    plugins: ['AMap.Marker', 'AMap.ToolBar', 'AMap.Scale'], // 所需插件
  })
    .then((AMap) => {
      // 创建地图实例
      mapInstance.value = new AMap.Map(mapContainer.value, {
        viewMode: '2D', // 默认使用2D模式
        zoom: 15, // 初始缩放级别
        center: location.value.position, // 初始中心点
      })
 
      // 添加控件
      mapInstance.value.addControl(new AMap.ToolBar())
      mapInstance.value.addControl(new AMap.Scale())
 
      // 添加标记点
      marker.value = new AMap.Marker({
        position: location.value.position,
        title: location.value.name,
      })
      mapInstance.value.add(marker.value)
 
      // 点击标记点显示信息
      marker.value.on('click', () => {
        alert(`您点击了:${location.value.name}`)
      })
    })
    .catch((error) => {
      console.error('地图加载失败:', error)
    })
})
</script>
 
<style scoped>
.map-container {
  position: relative;
  width: 100%;
  height: 500px;
}
 
#map-container {
  width: 100%;
  height: 100%;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}
 
.location-card {
  position: absolute;
  bottom: 20px;
  left: 20px;
  background: white;
  padding: 15px;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
  z-index: 10;
  max-width: 300px;
}
</style>