vue中引入高德地圖并多點(diǎn)標(biāo)注的實(shí)現(xiàn)步驟
vue中引入高德地圖并多點(diǎn)標(biāo)記
步驟:
- 通過(guò)vue的方法引入地圖
- 初始化地圖,設(shè)置寬和高
- 信息窗口實(shí)例
- 遍歷生成多個(gè)標(biāo)記點(diǎn)
首先在項(xiàng)目的public下的index.html中引入地圖
<link rel="stylesheet" /> <script src="https://webapi.amap.com/maps?v=1.4.15&key=申請(qǐng)的key"></script>
上代碼(注釋?zhuān)?/p>
<template>
<div>
//為地圖設(shè)置寬和高
<div id="container" style="width: 100%;height: 700px"></div>
</div>
</template>
<script>
export default {
data() {
return {
//假數(shù)據(jù) 經(jīng)緯度
lnglats: [
[113.922282,35.332887],
[113.963101,35.318516],
[113.960801,35.306263],
[113.926809,35.301255]
],
}
},
mounted() {
this.carGPSIP()
},
methods: {
carGPSIP() {
var map = new AMap.Map("container", {resizeEnable: true});//初始化地圖
//信息窗口實(shí)例
var infoWindow = new AMap.InfoWindow({offset: new AMap.Pixel(0, -30)});
//遍歷生成多個(gè)標(biāo)記點(diǎn)
for (var i = 0, marker; i < this.lnglats.length; i++) {
var marker = new AMap.Marker({
position: this.lnglats[i],//不同標(biāo)記點(diǎn)的經(jīng)緯度
map: map
});
marker.content = '我是第' + (i + 1) + '個(gè)Marker';
marker.on('click', markerClick);
marker.emit('click', {target: marker});//默認(rèn)初始化不出現(xiàn)信息窗體,打開(kāi)初始化就出現(xiàn)信息窗體
}
function markerClick(e) {
infoWindow.setContent(e.target.content);
infoWindow.open(map, e.target.getPosition());
}
map.setFitView();
}
},
}
</script>
<style>
</style>
補(bǔ)充:下面看下VUE+vue-amap實(shí)現(xiàn)高德地圖多個(gè)標(biāo)記點(diǎn)展示

安裝組件
npm install vue-amap --save
在main.js引入插件
import VueAMap from 'vue-amap'
Vue.use(VueAMap)
VueAMap.initAMapApiLoader({
key: '在高德地圖申請(qǐng)的key',
plugin: [
'AMap.DistrictSearch',
'AMap.Autocomplete', // 輸入提示插件
'AMap.PlaceSearch', // POI搜索插件
'AMap.Scale', // 右下角縮略圖插件 比例尺
'AMap.OverView', // 地圖鷹眼插件
'AMap.ToolBar', // 地圖工具條
'AMap.MapType', // 類(lèi)別切換控件,實(shí)現(xiàn)默認(rèn)圖層與衛(wèi)星圖、實(shí)施交通圖層之間切換的控制
'AMap.PolyEditor', // 編輯 折線(xiàn)多,邊形
'AMap.CircleEditor', // 圓形編輯器插件
'AMap.Geolocation', // 定位控件,用來(lái)獲取和展示用戶(hù)主機(jī)所在的經(jīng)緯度位置
'AMap.Geocoder'
],
v: '1.4.4',
uiVersion: '1.0.11'
})在index.vue中運(yùn)用
html部分代碼
<template>
<div>
<el-amap
vid="amapContainer"
:amap-manager="amapManager"
:events="events"
class="amap-demo"
>
</el-amap>
</div>
</template>js部分代碼
<script>
import { AMapManager, lazyAMapApiLoaderInstance } from 'vue-amap'
const amapManager = new AMapManager()
export default {
data() {
return {
//地圖列表
mapList: [],
amapManager,
}
},
//必須寫(xiě)在mounted方法中
mounted() {
lazyAMapApiLoaderInstance.load().then(() => {
this.map = new AMap.Map('amapContainer', {
center: [108.956673, 34.211891], //地圖顯示的中心位置
zoom: 5, //地圖縮放比例
mapStyle: 'amap://styles/9477331003fd4f8bd683a2450bd58adb', //自定義地圖皮膚,用的規(guī)劃夜皮膚
})
//地圖標(biāo)記點(diǎn)方法
this.markers()
})
},
methods: {
markers() {
// 標(biāo)記點(diǎn)未渲染完 login加載中
const loading = this.$loading({
lock: true,
text: 'Loading',
spinner: 'el-icon-loading',
background: 'rgba(0, 0, 0, 0.7)',
})
//地圖列表 從后臺(tái)獲取
let mapList = this.mapList
// 循環(huán)渲染標(biāo)記點(diǎn)
for (var i = 0; i < mapList.length; i++) {
// 獲取的經(jīng)緯度126.53,45.8
let position = mapList[j].lng + ',' + mapList[j].lat
//將字符串分割成數(shù)組形式 ["126.53", "45.8"]
position = position.split(',')
var text = new AMap.Text({
text: '.',
anchor: 'center', // 設(shè)置文本標(biāo)記錨點(diǎn)
draggable: false,
cursor: 'pointer',
angle: 10,
style: {
'width': '20px',
'height': '20px',
'border-radius': '50%', //設(shè)置為圓形
'background-color': mapList[i].colorStyle, //標(biāo)記點(diǎn)顏色
'border-color': mapList[i].colorStyle, //標(biāo)記點(diǎn)邊框顏色
'color':mapList[i].colorStyle, //文字顏色
'opacity': '0.8', //透明度
},
position: position, //圓點(diǎn)位置 經(jīng)緯度
//鼠標(biāo)放到圓點(diǎn)上顯示的信息
title: `
項(xiàng)目名稱(chēng):${mapList[i].proName}
智能單元編號(hào):${mapList[j].itNumber} `,
})
text.setMap(this.map)
//關(guān)閉加載框
setTimeout(() => {
loading.close()
}, 500)
}
},
//點(diǎn)擊左下角的設(shè)備列表聚焦到標(biāo)記點(diǎn)位置
focusing(id) {
// console.log(id) 獲取列表的經(jīng)緯度
let mapList = this.mapList
let arr = []
//循環(huán)地圖數(shù)據(jù)列表 經(jīng)緯度和左下角設(shè)備列表的經(jīng)緯度相同 就賦值到arr
for (var i = 0; i < mapList.length; i++) {
if (mapList[i].projectId == id) {
arr.push(mapList[i].lng + ',' + mapList[i].lat)
}
}
// 聚焦到標(biāo)記點(diǎn)位置的方法
this.map.setZoomAndCenter(10, arr[0].split(','))
},
}
</script>會(huì)出現(xiàn)的問(wèn)題
使用高德地圖自定義皮膚展示不出來(lái) 必須在public下的index.html添加以下代碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<link rel="icon" href="<%= BASE_URL %>bitbug_favicon.ico">
<title>設(shè)備地圖</title>
<script>
window._AMapSecurityConfig = {
securityJsCode:'高德地圖你申請(qǐng)的安全密鑰',
}
</script>
<body>
<script src="https://webapi.amap.com/maps?v=1.4.15&key=高德地圖申請(qǐng)的key&plugin=AMap.Geocoder"></script>
<script src="https://webapi.amap.com/ui/1.0/main.js?v=1.0.11"></script>
</body>
</html>到此這篇關(guān)于vue中引入高德地圖并多點(diǎn)標(biāo)注的文章就介紹到這了,更多相關(guān)vue高德地圖多點(diǎn)標(biāo)注內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue2.0實(shí)現(xiàn)倒計(jì)時(shí)的插件(時(shí)間戳 刷新 跳轉(zhuǎn) 都不影響)
我發(fā)現(xiàn)好多倒計(jì)時(shí)的插件,刷新都會(huì)變成從頭再來(lái),于是自己用vue2.0寫(xiě)了一個(gè),感覺(jué)還不錯(cuò),特此分享到腳本之家平臺(tái)供大家參考下2017-03-03
vue實(shí)現(xiàn)簡(jiǎn)單的跑馬燈效果
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)簡(jiǎn)單的跑馬燈效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10
在Vue3中實(shí)現(xiàn)四種全局狀態(tài)數(shù)據(jù)的統(tǒng)一管理的方法
在開(kāi)發(fā)中,通常遇到四種全局狀態(tài)數(shù)據(jù):異步數(shù)據(jù)、同步數(shù)據(jù),傳統(tǒng)的Vue3使用不同機(jī)制處理這些數(shù)據(jù),而Zova框架通過(guò)Model機(jī)制來(lái)統(tǒng)一管理,簡(jiǎn)化了數(shù)據(jù)處理流程,提高了代碼的可維護(hù)性,本文介紹在Vue3中實(shí)現(xiàn)四種全局狀態(tài)數(shù)據(jù)的統(tǒng)一管理的方法,感興趣的朋友一起看看吧2024-10-10
Vue偵測(cè)相關(guān)api的實(shí)現(xiàn)方法
這篇文章主要介紹了Vue偵測(cè)相關(guān)api,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05
vue項(xiàng)目中使用particles實(shí)現(xiàn)粒子背景效果及遇到的坑(按鈕沒(méi)有點(diǎn)擊響應(yīng))
為了提高頁(yè)面展示效果,登錄界面內(nèi)容比較單一的,粒子效果作為背景經(jīng)常使用到,vue工程中利用vue-particles可以很簡(jiǎn)單的實(shí)現(xiàn)頁(yè)面的粒子背景效果,本文給大家分享在實(shí)現(xiàn)過(guò)程中遇到問(wèn)題,需要的朋友一起看看吧2020-02-02
Vue系列:通過(guò)vue-router如何傳遞參數(shù)示例
本篇文章主要介紹了Vue系列:通過(guò)vue-router如何傳遞參數(shù)示例,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-01-01

