Vue引入高德地圖并觸發(fā)實現(xiàn)多個標點的示例詳解
更新時間:2022年05月28日 10:02:27 作者:張清悠
這篇文章主要介紹了Vue引入高德地圖并觸發(fā)實現(xiàn)多個標點,主要是在public下的index.html中引入地圖,引入組件設(shè)置寬高100%,本文通過示例代碼給大家介紹的非常詳細,需要的朋友可以參考下
1、 在public下的index.html中引入地圖
<link rel="stylesheet" rel="external nofollow" /> <script src="https://webapi.amap.com/maps?v=1.4.15&key=申請的key"></script>
2、引入組件設(shè)置寬高100%
<template>
<div>
<div id="container" style="width: 100%;height: 550px"></div>
</div>
</template>3、數(shù)組形式數(shù)據(jù)固定(一)
<script>
export default {
data() {
return {
//要標記的所有點的經(jīng)緯度
lnglats: [
[108.909074, 34.254225],
[108.910766, 34.254348],
[108.910495, 34.253531],
[108.909502, 34.253571],
],
}
},
mounted() {
this.carGPSIP()
},
methods: {
carGPSIP() {
var map = new AMap.Map("container", {resizeEnable: true});//初始化地圖
//信息窗口實例
var infoWindow = new AMap.InfoWindow({offset: new AMap.Pixel(0, -30)});
//遍歷生成多個標記點
for (var i = 0, marker; i < this.lnglats.length; i++) {
var marker = new AMap.Marker({
position: this.lnglats[i],//不同標記點的經(jīng)緯度
map: map
});
marker.content = '我是第' + (i + 1) + '個Marker';
marker.on('click', markerClick);
marker.emit('click', {target: marker});//默認初始化不出現(xiàn)信息窗體,打開初始化就出現(xiàn)信息窗體
}
function markerClick(e) {
infoWindow.setContent(e.target.content);
infoWindow.open(map, e.target.getPosition());
}
map.setFitView();
}
},
}
</script>4、用ajax請求后端真是接口(二)
<template>
<div id="container" style="width: 100%;height: 550px"></div>
<!-- 設(shè)置寬和高 -->
</template>
<script>
export default {
data() {
return {
//要標記的所有點的經(jīng)緯度
Coordinate:[]
// Coordinate:[
// {
// lng:"54.323243",
// lat:"43.654322"
// }
// ] //后端返回的數(shù)據(jù)格式
}
},
mounted() {
this.carGPSIP()
},
methods: {
carGPSIP() {
var map = new AMap.Map("container", {resizeEnable: true});//初始化地圖
//信息窗口實例
var infoWindow = new AMap.InfoWindow({offset: new AMap.Pixel(0, -30)});
//遍歷生成多個標記點 因后端返回是map格式因此需要判斷code
$ajax.positionType({}, ({ code, data }) => {
if (code == 200) {
console.log(data);
this. Coordinate = data.deviceList; //拿到數(shù)據(jù)
let Coordinate = data.deviceList; //定義Coordinate
for (var i = 0; i < this. Coordinate.length; i++) {
var marker = new AMap.Marker({
position: new AMap.LngLat( Coordinate[i].lng, Coordinate[i].lat), //不同標記點的經(jīng)緯度
map: map,
});
marker.content = '我是第' + (i + 1) + '個Marker';
marker.on("click", markerClick);
marker.emit("click", { target: marker }); //默認初始化不出現(xiàn)信息窗體,打開初始化就出現(xiàn)信息窗體
}
function markerClick(e) {
infoWindow.setContent(e.target.content);
infoWindow.open(map, e.target.getPosition());
}
map.setFitView();
}
});
function markerClick(e) {
infoWindow.setContent(e.target.content);
infoWindow.open(map, e.target.getPosition());
}
map.setFitView();
}
},
}
</script>
<style>
</style>5、其他需求請看文檔請看官方文檔
綜上就是簡答使用高德地圖分全部過程,具體需求請參照高德官方api
到此這篇關(guān)于Vue引入高德地圖并觸發(fā)實現(xiàn)多個標點的文章就介紹到這了,更多相關(guān)vue高德地圖觸發(fā)多個標點內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue面試created中兩次數(shù)據(jù)修改會觸發(fā)幾次頁面更新詳解
這篇文章主要為大家介紹了vue面試created中兩次數(shù)據(jù)修改會觸發(fā)幾次頁面更新問題解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-12-12
想到頭禿也想不到的Vue3復(fù)用組件還可以這么hack的用法
這篇文章主要為大家介紹了想到頭禿也想不到的Vue3復(fù)用組件還可以這么hack的用法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-04-04
vue?el-switch初始值(默認值)不能正確顯示狀態(tài)問題及解決
這篇文章主要介紹了vue?el-switch初始值(默認值)不能正確顯示狀態(tài)問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10
vue循環(huán)中點擊選中再點擊取消(單選)的實現(xiàn)
這篇文章主要介紹了vue循環(huán)中點擊選中再點擊取消(單選)的實現(xiàn),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09

