高德地图 vue3 + ts 的基础使用
<template>
<div style="width: 100%; height: 100%" id="map"></div>
</template>
<script lang="ts" setup>
import { onMounted, ref } from 'vue'
interface ILocation {
longitude: string | number
latitude: string | number
}
const key = ''
const nMap = ref('')
const defaultStyle = {
zoom: 11,
center: [120.7170619747857, 30.73945447736177],
viewMode: '2D'
}
onMounted(async () => {
await loadScript()
})
const loadScript = () => {
const url = `https://webapi.amap.com/maps?v=2.0&key=${key}&callback=onLoad`
const jsapi = document.createElement('script')
jsapi.charset = 'utf-8'
jsapi.src = url
document.head.appendChild(jsapi)
jsapi.onload = () => {
initMap()
}
}
const initMap = () => {
nMap.value = new AMap.Map('map', defaultStyle)
}
const addPointMarker = (longitude: number, latitude: number) => {
const marker = new AMap.Marker({
position: new AMap.LngLat(longitude, latitude),
title: ' '
})
nMap.value.add(marker)
}
const addPointMarkerList = (arr: Array<Array<number | string>>) => {
let markerList = ref<any>([])
arr.forEach(item => {
const marker = new AMap.Marker({
position: new AMap.LngLat(item[0], item[1]),
title: ' '
})
console.log(marker)
markerList.value.push(marker)
})
nMap.value.add(markerList.value)
}
</script>