高德地图 vue3 + ts 的基础使用

高德地图 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 = '' // 自己申请的高德地图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()
	}
}
/**
 * 实例化AMap对象
 */
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>

使用高德地图实现模糊搜索定位,你需要先在项目中引入高德地图JavaScript API,并且需要获取高德地图的 API key。接下来,你可以使用 Autocomplete 插件实现模糊搜索,具体实现步骤如下: 1. 在 Vue 3 中安装高德地图JavaScript API。 ```bash npm install @amap/amap-jsapi-loader --save ``` 2. 在 main.ts 文件中引入高德地图JavaScript API,并在创建 Vue 实例之前加载 JavaScript API。 ```typescript import { createApp } from 'vue' import App from './App.vue' import AMapLoader from '@amap/amap-jsapi-loader' AMapLoader.load({ key: 'your_amap_api_key', version: '2.0', plugins: ['AMap.Autocomplete'] }).then(() => { createApp(App).mount('#app') }) ``` 3. 在组件中使用 Autocomplete 插件实现模糊搜索。 ```vue <template> <div> <input v-model="address" placeholder="请输入地址" /> <ul> <li v-for="suggestion in suggestions" :key="suggestion.id">{{ suggestion.name }}</li> </ul> </div> </template> <script lang="ts"> import { defineComponent, ref } from 'vue' import AMapLoader from '@amap/amap-jsapi-loader' export default defineComponent({ name: 'AutoCompleteDemo', setup() { const address = ref('') const suggestions = ref([]) const search = async () => { const AMap = await AMapLoader.load({ key: 'your_amap_api_key', version: '2.0', plugins: ['AMap.Autocomplete'] }) const autoComplete = new AMap.Autocomplete({ city: '全国' }) autoComplete.search(address.value, (status, result) => { if (status === 'complete' && result.tips) { suggestions.value = result.tips } }) } return { address, suggestions, search } } }) </script> ``` 在这个例子中,我们使用了 ref 函数来创建了两个响应式变量 address 和 suggestions,分别用于存储输入的地址和搜索结果。我们还定义了一个 search 函数,在用户输入时触发,它会通过 AMapLoader.load() 方法加载 Autocomplete 插件,并使用 Autocomplete.search() 方法进行模糊搜索。当搜索完成时,Autocomplete.search() 的回调函数会返回搜索结果,我们将结果存储在 suggestions 变量中,并在页面中渲染出来。 注意,这里我们设置了 city 属性为全国,表示搜索范围为全国。你也可以根据需要修改为特定的城市或区域。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值