地图显示
TIP
地图显示。
代码如下:
点我查看代码
vue
<template>
<div id="map"></div>
</template>
<script lang="ts" setup>
import { onMounted, onBeforeUnmount } from "vue";
import L from "leaflet";
import { MAPURL, ATTRIBUTIONS } from "../../../constants";
let map: L.Map | null = null;
const initMap = () => {
//地图容器
map = L.map("map").setView([22.548857, 114.064839], 10);
//加载图层
L.tileLayer(MAPURL, {
noWrap: true,
attribution: ATTRIBUTIONS,
}).addTo(map);
//添加标注
L.marker([22.548857, 114.064839])
.addTo(map)
.bindPopup("您好!深圳")
.openPopup();
};
onMounted(() => {
initMap();
});
// 销毁地图
onBeforeUnmount(() => {
if (map) {
map.remove();
map = null;
}
});
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
#map {
height: 650px;
}
</style>