shuishen
2025-09-28 b4e7fc334e6b1e1844a812ebf6b3924f70dc1b9e
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
<template>
  <!-- 地图容器 -->
  <view id="map" class="map" style="width: 100%; height: calc(100vh - 10px);"></view>
</template>
 
<script setup>
import { onMounted } from 'vue'
import 'leaflet/dist/leaflet.css'
import L from 'leaflet'
 
// 页面加载完成后初始化地图
onMounted(() => {
  // 1. 创建地图并设置中心点和缩放等级
  const map = L.map('map').setView([31.2304, 121.4737], 13) // [纬度, 经度], 缩放
 
  // 2. 添加底图图层(OpenStreetMap)
  L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
    maxZoom: 19,
    attribution: '&copy; OpenStreetMap contributors'
  }).addTo(map)
 
  // 3. 添加一个 Marker 示例
  const marker = L.marker([31.2304, 121.4737]).addTo(map)
  marker.bindPopup('<b>Hello!</b><br/>这是一个Leaflet地图点位').openPopup()
})
</script>
 
<style scoped>
/* Leaflet 默认容器要有高度 */
.map {
  width: 100%;
  height: 100%;
}
</style>