新建文件 amap.vue
:
<template>
<div id="amapcontainer" style="width: 1200px; height: 720px"></div>
</template>
<script>
import AMapLoader from '@amap/amap-jsapi-loader';
window._AMapSecurityConfig = {
securityJsCode: '' // '「申請的安全密鑰」',
}
export default {
data () {
return {
map: null,
markerList: [],
infoWindow: '',
mapList: [
{
name: '小王',
address: '廣東省廣州市海珠區(qū)',
lnglats: [113.312566, 23.085073]
}, {
name: '小張',
address: '廣東省廣州市黃埔區(qū)',
lnglats: [113.480794, 23.137896]
}, {
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: "", // 申請好的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: 10, // 地圖顯示的縮放級別
viewMode: "3D", // 使用3D視圖
zoomEnable: true, // 地圖是否可縮放,默認(rèn)值為true
dragEnable: true, // 地圖是否可通過鼠標(biāo)拖拽平移,默認(rèn)為true
doubleClickZoom: true, // 地圖是否可通過雙擊鼠標(biāo)放大地圖,默認(rèn)為true
zoom: 11, //初始化地圖級別
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) // 圖像相對展示區(qū)域的偏移量,適于雪碧圖等
});
//信息窗口實(shí)例
this.infoWindow = new AMap.InfoWindow({
anchor: "top-left",
offset: new AMap.Pixel(0, -30)
});
let makerList = []
this.mapList.forEach((item, i) => {
// 遍歷生成多個(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)緯度對象new AMap.LngLat(x, y),也可以是經(jīng)緯度構(gòu)成的一維數(shù)組[116.39, 39.9]
});
let content =
'<ul style=" margin:-10px 0 5px 0; padding:0.2em 0;">'
+ '<li style="font-size:14px;color:#727272;">'
+ '<span style="width:50px; display:inline-block;">第</span>'
+ i + '條</li>'
+ '<li style="line-height:26px; font-size:14px;color:#727272;margin-bottom:6px">'
+ '<span style="width:50px; display:inline-block;">姓 名:</span>'
+ item.name + '</li>'
+ '<li style="font-size:14px;color:#727272;">'
+ '<span style="width:50px; display:inline-block;" >地 址:</span>'
+ item.address + '</li>'
+ '</ul>'
marker.content = content
marker.on("click", this.markerClick)
marker.emit('click', { target: marker });// 此處是設(shè)置默認(rèn)出現(xiàn)信息窗體
makerList.push(marker)
});
this.makerList = makerList
this.map.add(makerList)
// 自動適應(yīng)顯示想顯示的范圍區(qū)域
this.map.setFitView();
},
// 控制標(biāo)記的信息窗體的顯示
markerClick (e) {
// 標(biāo)注的點(diǎn)擊事件
this.infoWindow.setContent(e.target.content);
this.infoWindow.open(this.map, e.target.getPosition());
}
}
}
</script>
<style lang="less">
</style>
在需要使用的組件中引入 amap.vue
:文章來源:http://www.zghlxwxcb.cn/news/detail-522277.html
<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>
頁面效果:文章來源地址http://www.zghlxwxcb.cn/news/detail-522277.html
到了這里,關(guān)于vue 高德地圖點(diǎn)擊標(biāo)記點(diǎn)彈出對應(yīng)信息彈窗的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!