vue-cli項(xiàng)目h5頁面或者PC端頁面引入高德地圖組件辰狡,多點(diǎn)標(biāo)注宛篇,自定義彈窗的詳細(xì)描述

1.index.html里引入標(biāo)簽:

<script type="text/javascript" src="https://webapi.amap.com/maps?v=1.3&key=yourKey"></script>
<script src="https://webapi.amap.com/ui/1.0/main.js?v=1.0.11"></script>

2.新建一個(gè)js文件(mapConf.js)

export default function MapLoader () { // <-- 原作者這里使用的是module.exports

return new Promise((resolve, reject) => {

if (window.AMap) {
  resolve(window.AMap)
} else {
    var script = document.createElement('script')
    script.type = 'text/javascript'
    script.async = true
    script.src = 'http://webapi.amap.com/maps?v=1.3&callback=initAMap&key=yourKey'
    script.onerror = reject
    document.head.appendChild(script)
}

window.initAMap = () => {
  resolve(window.AMap)
}
})
}

3.將新建的js文件引入地圖頁面

import MapLoader from '../../../common/mapConf' 即:實(shí)例化Amap對(duì)象

初始化顯示map 需要定義center(中心點(diǎn)),zoom(顯示地圖級(jí)別 可以理解為縮放比例)。

MapLoader().then(AMap => {
  that.map = new AMap.Map('container', { //“container”是html定義的地圖組件的id
  center: [116.42894, 39.894624],
  zoom: 15,
  resizeEnable: true
})
AMap.plugin([
  'AMap.ToolBar',
], function(){

  // 在圖面添加工具條控件吆倦,工具條控件集成了縮放蚕泽、平移须妻、定位等功能按鈕在內(nèi)的組合控件
  that.map.addControl(new AMap.ToolBar({

// 簡易縮放模式荒吏,默認(rèn)為 false
liteStyle: true

}));

});

 //添加多個(gè)點(diǎn)標(biāo)注跟自定義窗體信息方法
this.listenerFuncMap()

}, e => {

})

listenerFuncMap代碼

listenerFuncMap(){

let self =this;

AMapUI.loadUI(['misc/MarkerList', 'overlay/SimpleMarker', 'overlay/SimpleInfoWindow'], function(MarkerList, SimpleMarker, SimpleInfoWindow) {

var markerList = new MarkerList({

//關(guān)聯(lián)的map對(duì)象

map: self.map,

//選中狀態(tài)(通過點(diǎn)擊列表或者marker)時(shí)在Marker和列表節(jié)點(diǎn)上添加的class司倚,可以借此編寫css控制選中時(shí)的展示效果

selectedClassNames: 'my-active',

//返回?cái)?shù)據(jù)項(xiàng)的Id

getDataId: function(dataItem, index) {

//index表示該數(shù)據(jù)項(xiàng)在數(shù)組中的索引位置皿伺,從0開始鸵鸥,如果確實(shí)沒有id妒穴,可以返回index代替

return dataItem.id;

},

//返回?cái)?shù)據(jù)項(xiàng)的位置信息讼油,需要是AMap.LngLat實(shí)例矮台,或者是經(jīng)緯度數(shù)組瘦赫,比如[116.789806, 39.904989]

getPosition: function(dataItem) {

return dataItem.position;

},

//返回?cái)?shù)據(jù)項(xiàng)對(duì)應(yīng)的Marker

getMarker: function(dataItem, context, recycledMarker) {

var label = {

offset: new AMap.Pixel(16, 18), //修改label相對(duì)于marker的位置

};

//存在可回收利用的marker

if (recycledMarker) {

//直接更新內(nèi)容返回

recycledMarker.setLabel(label);

return recycledMarker;

}

//返回一個(gè)新的Marker

if(dataItem.nameDesc=="技師:"){

return new AMap.Marker({

label: label,

icon:new AMap.Icon({

image:"http://image.uservices.cn/tmp/uploads/fws/20181214/5c136a64111b8.png",

size:new AMap.Size(50,50),

imageSize:new AMap.Size(50,50)

}),

});

}else{

return new AMap.Marker({

label: label,

icon:new AMap.Icon({

image:dataItem.headerImg?dataItem.headerImg:"http://image.uservices.cn/tmp/uploads/fws/20181214/5c13693c4b239.png",

size:new AMap.Size(50,50),

imageSize:new AMap.Size(50,50)

}),

});

}

},

//返回?cái)?shù)據(jù)項(xiàng)對(duì)應(yīng)的infoWindow

getInfoWindow: function(dataItem, context, recycledInfoWindow) {

if (recycledInfoWindow) {

recycledInfoWindow.setInfoTitle(dataItem.nameDesc+(dataItem.name));

recycledInfoWindow.setInfoBody("<p style='margin: 0;padding: 0;'>"+dataItem.addressDesc+dataItem.address+"</p><p style='margin: 0;padding: 0;'>"+dataItem.lastTimeDesc+dataItem.lastTime+"</p>");

return recycledInfoWindow;

}

return new SimpleInfoWindow({

infoTitle: dataItem.name,

infoBody: dataItem.address,

offset: new AMap.Pixel(0, -30)

});

},

});

//監(jiān)聽選中改變

markerList.on('selectedChanged', function(event, info) {

//console.log(event, info);

});

//構(gòu)建一個(gè)數(shù)據(jù)項(xiàng)數(shù)組,數(shù)據(jù)項(xiàng)本身沒有格式要求,但需要支持下述的getDataId和getPosition

var data = [{

nameDesc:"技師:",

name: "張三",

position: [118.42894, 39.894624],

address:"啦啦啦",

addressDesc:"地址:",

lastTime:"8102-10-17",

lastTimeDesc:"獲取時(shí)間:",

},{

nameDesc:"聯(lián)系人:",

name: "張燕妮",

position: [116.22894, 39.894624],

address:"哈哈哈",

addressDesc:"地址:",

lastTime:"110",

lastTimeDesc:"電話:",

headerImg:""

}];

//展示該數(shù)據(jù)

markerList.render(data);

});

self.map.setFitView(); //是標(biāo)注的點(diǎn)全都自適應(yīng)初始化顯示在地圖上

},

如圖:

clipboard.png

4.em.....奉上所有代碼


<div class="wrapper">

<div id="container"></div>

</div>

</template>

<script>

import MapLoader from '../../../common/mapConf'

import api from '../api/api';

export default {

data() {

return {

center: [116.42894, 39.894624],

zoom: 15,

markers:[],

map:null,

detailData:"",//詳細(xì)信息

}

},

methods:{

initMap(){

this.getEngineerAndCustomerPosFunc()

},

listenerFuncMap(){

let self =this;

AMapUI.loadUI(['misc/MarkerList', 'overlay/SimpleMarker', 'overlay/SimpleInfoWindow'], function(MarkerList, SimpleMarker, SimpleInfoWindow) {

var markerList = new MarkerList({

//關(guān)聯(lián)的map對(duì)象

map: self.map,

//選中狀態(tài)(通過點(diǎn)擊列表或者marker)時(shí)在Marker和列表節(jié)點(diǎn)上添加的class召川,可以借此編寫css控制選中時(shí)的展示效果

selectedClassNames: 'my-active',

//返回?cái)?shù)據(jù)項(xiàng)的Id

getDataId: function(dataItem, index) {

//index表示該數(shù)據(jù)項(xiàng)在數(shù)組中的索引位置,從0開始胸遇,如果確實(shí)沒有id荧呐,可以返回index代替

return dataItem.id;

},

//返回?cái)?shù)據(jù)項(xiàng)的位置信息,需要是AMap.LngLat實(shí)例纸镊,或者是經(jīng)緯度數(shù)組倍阐,比如[116.789806, 39.904989]

getPosition: function(dataItem) {

return dataItem.position;

},

//返回?cái)?shù)據(jù)項(xiàng)對(duì)應(yīng)的Marker

getMarker: function(dataItem, context, recycledMarker) {

var label = {

offset: new AMap.Pixel(16, 18), //修改label相對(duì)于marker的位置

};

//存在可回收利用的marker

if (recycledMarker) {

//直接更新內(nèi)容返回

recycledMarker.setLabel(label);

return recycledMarker;

}

//返回一個(gè)新的Marker

if(dataItem.nameDesc=="技師:"){

return new AMap.Marker({

label: label,

icon:new AMap.Icon({

image:"http://image.uservices.cn/tmp/uploads/fws/20181214/5c136a64111b8.png",

size:new AMap.Size(50,50),

imageSize:new AMap.Size(50,50)

}),

});

}else{

return new AMap.Marker({

label: label,

icon:new AMap.Icon({

image:dataItem.headerImg?dataItem.headerImg:"http://image.uservices.cn/tmp/uploads/fws/20181214/5c13693c4b239.png",

size:new AMap.Size(50,50),

imageSize:new AMap.Size(50,50)

}),

});

}

},

//返回?cái)?shù)據(jù)項(xiàng)對(duì)應(yīng)的infoWindow

getInfoWindow: function(dataItem, context, recycledInfoWindow) {

if (recycledInfoWindow) {

recycledInfoWindow.setInfoTitle(dataItem.nameDesc+(dataItem.name));

recycledInfoWindow.setInfoBody("<p style='margin: 0;padding: 0;'>"+dataItem.addressDesc+dataItem.address+"</p><p style='margin: 0;padding: 0;'>"+dataItem.lastTimeDesc+dataItem.lastTime+"</p>");

return recycledInfoWindow;

}

return new SimpleInfoWindow({

infoTitle: dataItem.name,

infoBody: dataItem.address,

offset: new AMap.Pixel(0, -30)

});

},

});

//監(jiān)聽選中改變

markerList.on('selectedChanged', function(event, info) {

//console.log(event, info);

});

//構(gòu)建一個(gè)數(shù)據(jù)項(xiàng)數(shù)組,數(shù)據(jù)項(xiàng)本身沒有格式要求逗威,但需要支持下述的getDataId和getPosition

if(self.detailData){

var data = [{

nameDesc:"技師:",

name: self.detailData.technician.name,

position: [self.detailData.technician.lon, self.detailData.technician.lat],

address:self.detailData.technician.address,

addressDesc:"地址:",

lastTime:self.detailData.technician.lastTime,

lastTimeDesc:"獲取時(shí)間:",

},{

nameDesc:"聯(lián)系人:",

name: self.detailData.account.name,

position: [self.detailData.account.lon, self.detailData.account.lat],

address:self.detailData.account.address,

addressDesc:"地址:",

lastTime:self.detailData.account.mobile,

lastTimeDesc:"電話:",

headerImg:self.detailData.account.avatar_hc

}];

}

//展示該數(shù)據(jù)

markerList.render(data);

});

self.map.setFitView();

},

//初始化獲取位置

getEngineerAndCustomerPosFunc(){

api.getEngineerAndCustomerPosAxios(params).then((res)=>{

if(res.code==200){

this.detailData = res.data;

let that = this;

MapLoader().then(AMap => {

that.map = new AMap.Map('container', {

center: [116.42894, 39.894624],

zoom: 15,

resizeEnable: true

})

AMap.plugin([

'AMap.ToolBar',

], function(){

// 在圖面添加工具條控件峰搪,工具條控件集成了縮放、平移凯旭、定位等功能按鈕在內(nèi)的組合控件

that.map.addControl(new AMap.ToolBar({

// 簡易縮放模式,默認(rèn)為 false

liteStyle: true

}));

});

this.listenerFuncMap()

}, e => {

})

}else{

this.$toast.center("信息獲取失敗")

}

})

}

},

created(){

this.initMap();

document.title = '技師位置';

},

}

</script>

<style scoped>

.wrapper{

position:absolute;

width:100%;

height:100%;

}

#container{

width: 100%;

height: 100%;

}

</style>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末奉呛,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌罗晕,老刑警劉巖茫叭,帶你破解...
    沈念sama閱讀 221,548評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件莽囤,死亡現(xiàn)場離奇詭異话肖,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)辙培,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,497評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來蛮穿,“玉大人灸异,你說我怎么就攤上這事∨迸” “怎么了?”我有些...
    開封第一講書人閱讀 167,990評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵缘圈,是天一觀的道長廷没。 經(jīng)常有香客問我另锋,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,618評(píng)論 1 296
  • 正文 為了忘掉前任仁热,我火速辦了婚禮思劳,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘森瘪。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,618評(píng)論 6 397
  • 文/花漫 我一把揭開白布昔穴。 她就那樣靜靜地躺著狈网,像睡著了一般窥摄。 火紅的嫁衣襯著肌膚如雪币砂。 梳的紋絲不亂的頭發(fā)上玻侥,一...
    開封第一講書人閱讀 52,246評(píng)論 1 308
  • 那天姑食,我揣著相機(jī)與錄音音半,去河邊找鬼。 笑死坛善,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 40,819評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼蟀俊!你這毒婦竟也來了钦铺?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,725評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤肢预,失蹤者是張志新(化名)和其女友劉穎矛洞,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體误甚,經(jīng)...
    沈念sama閱讀 46,268評(píng)論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,356評(píng)論 3 340
  • 正文 我和宋清朗相戀三年谱净,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了窑邦。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,488評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡壕探,死狀恐怖冈钦,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情李请,我是刑警寧澤瞧筛,帶...
    沈念sama閱讀 36,181評(píng)論 5 350
  • 正文 年R本政府宣布厉熟,位于F島的核電站,受9級(jí)特大地震影響较幌,放射性物質(zhì)發(fā)生泄漏揍瑟。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,862評(píng)論 3 333
  • 文/蒙蒙 一乍炉、第九天 我趴在偏房一處隱蔽的房頂上張望绢片。 院中可真熱鬧,春花似錦岛琼、人聲如沸底循。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,331評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽熙涤。三九已至,卻和暖如春困檩,著一層夾襖步出監(jiān)牢的瞬間祠挫,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,445評(píng)論 1 272
  • 我被黑心中介騙來泰國打工窗看, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留茸歧,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,897評(píng)論 3 376
  • 正文 我出身青樓显沈,卻偏偏與公主長得像软瞎,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子拉讯,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,500評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容