vue實(shí)現(xiàn)高德地圖添加多個(gè)點(diǎn)標(biāo)記
新建文件 amap.vue:
<template>
<div id="amapcontainer" style="width: 1000px; height: 720px"></div>
</template>
<script>
import AMapLoader from '@amap/amap-jsapi-loader';
window._AMapSecurityConfig = {
securityJsCode: '' // '「申請(qǐng)的安全密鑰」',
}
export default {
data () {
return {
map: null,
markerList: [],
mapList: [
{
name: '小王',
address: '廣東省廣州市海珠區(qū)',
lnglats: [113.312566, 23.085073]
}, {
name: '小張',
address: '廣東省廣州市黃埔區(qū)',
lnglats: [113.480794, 23.177896]
}, {
name: '小李',
address: '廣東省廣州市荔灣區(qū)',
lnglats: [113.220556, 23.10718]
},
{
name: '小趙',
address: '廣東省廣州市天河區(qū)',
lnglats: [113.365438, 23.124231]
}
]
}
},
mounted () {
// DOM初始化完成進(jìn)行地圖初始化
this.initAMap()
},
methods: {
initAMap () {
AMapLoader.load({
key: "", // 申請(qǐng)好的Web端開發(fā)者Key,首次調(diào)用 load 時(shí)必填
version: "2.0", // 指定要加載的 JSAPI 的版本,缺省時(shí)默認(rèn)為 1.4.15
plugins: ["AMap.Scale", "AMap.ToolBar", "AMap.ControlBar", 'AMap.Geocoder', 'AMap.Marker',
'AMap.CitySearch', 'AMap.Geolocation', 'AMap.AutoComplete', 'AMap.InfoWindow'], // 需要使用的的插件列表,如比例尺'AMap.Scale'等
}).then((AMap) => {
// 獲取到作為地圖容器的DOM元素,創(chuàng)建地圖實(shí)例
this.map = new AMap.Map("amapcontainer", { //設(shè)置地圖容器id
resizeEnable: true,
zoom: this.zoom, // 地圖顯示的縮放級(jí)別
viewMode: "3D", // 使用3D視圖
zoomEnable: true, // 地圖是否可縮放,默認(rèn)值為true
dragEnable: true, // 地圖是否可通過鼠標(biāo)拖拽平移,默認(rèn)為true
doubleClickZoom: true, // 地圖是否可通過雙擊鼠標(biāo)放大地圖,默認(rèn)為true
zoom: 11, //初始化地圖級(jí)別
center: [113.370824, 23.131265], // 初始化中心點(diǎn)坐標(biāo) 廣州
// mapStyle: "amap://styles/darkblue", // 設(shè)置顏色底層
})
this.setMapMarker()
}).catch(e => {
console.log(e)
})
},
// 增加點(diǎn)標(biāo)記
setMapMarker () {
// 創(chuàng)建 AMap.Icon 實(shí)例
let icon = new AMap.Icon({
size: new AMap.Size(36, 36), // 圖標(biāo)尺寸
image: "http://a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-red.png", // Icon的圖像
imageSize: new AMap.Size(24, 30), // 根據(jù)所設(shè)置的大小拉伸或壓縮圖片
imageOffset: new AMap.Pixel(0, 0) // 圖像相對(duì)展示區(qū)域的偏移量,適于雪碧圖等
});
let makerList = []
this.mapList.forEach((item) => {
// 遍歷生成多個(gè)標(biāo)記點(diǎn)
let marker = new AMap.Marker({
map: this.map,
zIndex: 9999999,
icon: icon, // 添加 Icon 實(shí)例
offset: new AMap.Pixel(-13, -30), //icon中心點(diǎn)的偏移量
position: item.lnglats // 經(jīng)緯度對(duì)象new AMap.LngLat(x, y),也可以是經(jīng)緯度構(gòu)成的一維數(shù)組[116.39, 39.9]
});
makerList.push(marker)
});
this.map.add(makerList)
// 自動(dòng)適應(yīng)顯示想顯示的范圍區(qū)域
this.map.setFitView();
}
}
}
</script>
<style lang="less">
</style>在需要使用的組件中引入 amap.vue:
<template>
<div>
<map-container></map-container>
</div>
</template>
<script>
import MapContainer from "@/components/amap";
export default {
name: "purchannel",
components: { MapContainer },
data () {
return {
}
},
watch: {},
created () { },
mounted () { },
methods: {
}
}
</script>
<style lang="less" scoped>
</style>頁(yè)面效果:

總結(jié)
到此這篇關(guān)于vue實(shí)現(xiàn)高德地圖添加多個(gè)點(diǎn)標(biāo)記的文章就介紹到這了,更多相關(guān)vue高德地圖添加點(diǎn)標(biāo)記內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
前端vue面試總問watch和computed區(qū)別及建議總結(jié)
在現(xiàn)代前端的面試中,vue和react是面試過程中基本必問的技術(shù)棧,其中Vue響應(yīng)式話題,watch和computed是面試官非常喜歡聊的主題,雖然watch和computed它們都用于監(jiān)聽數(shù)據(jù)的變化,但它們?cè)趯?shí)現(xiàn)原理、使用場(chǎng)景和行為上有著顯著的區(qū)別,本文將深入探討,并提供一些面試過程中的建議2023-10-10
vue forEach循環(huán)數(shù)組拿到自己想要的數(shù)據(jù)方法
今天小編就為大家分享一篇vue forEach循環(huán)數(shù)組拿到自己想要的數(shù)據(jù)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-09-09
vue-star評(píng)星組件開發(fā)實(shí)例
下面小編就為大家分享一篇vue-star評(píng)星組件開發(fā)實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-03-03
Vue中通過vue-router實(shí)現(xiàn)命名視圖的問題
這篇文章主要介紹了在Vue中通過vue-router實(shí)現(xiàn)命名視圖,本文給大家提到了vue-router的原理解析,給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
Vue.js計(jì)算屬性的變化監(jiān)聽的示例詳解
在Vue.js中,計(jì)算屬性(computed properties)是組件的重要組成部分,它們根據(jù)組件的數(shù)據(jù)自動(dòng)計(jì)算新的值,并在數(shù)據(jù)變化時(shí)自動(dòng)更新,本文將深入探討如何在Vue.js中監(jiān)聽計(jì)算屬性的變化,幫助你更好地理解Vue.js的響應(yīng)式系統(tǒng),需要的朋友可以參考下2025-03-03
vue2響應(yīng)式原理之Object.defineProperty()方法的使用
這篇文章主要介紹了vue2響應(yīng)式原理之Object.defineProperty()方法的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
vue3.0使用taro-ui-vue3引入組件不生效的問題及解決
這篇文章主要介紹了vue3.0使用taro-ui-vue3引入組件不生效的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
Vue3生命周期Hooks原理與調(diào)度器Scheduler關(guān)系
這篇文章主要為大家介紹了Vue3生命周期Hooks原理與調(diào)度器Scheduler關(guān)系詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07

