首先需要在index.html中引入高德地图的js链接,key需要换成你自己的key
最近有个需求是实现一个使用地图搜索定位的功能,在网上参考了下其他的文章,感觉不是很完善,自己整理了一下,可以实现点击定位,搜索列表定位等功能,可能有些地方是多余的,需要的自己看着改下
<script type="text/javascript" src="/UploadFiles/2021-04-02/maps">
效果图如下
下边就是实现过程
html部分
<template> <div id="wrap"> <div id="searchWrap"> <div class="searchWrap"> <input type="text" v-model="address" @input="search"><button @click="search">搜索</button> </div> <div id="result" class="amap_lib_placeSearch" v-show="hide"> <div class="amap_lib_placeSearch_list amap-pl-pc" v-for="(item,index) in poiArr" @click="openMarkerTipById(index,$event)" @mouseout="onmouseout_MarkerStyle(index+1,$event)" :key="index"> <div class="poibox" style="border-bottom: 1px solid #eaeaea"> <div class="amap_lib_placeSearch_poi poibox-icon" :class="index==selectedIndex">{{index+1}}</div> <div class="poi-img" v-if="item.url" :style="'background-image:url('+item.url+'" ></div> <h3 class="poi-title" > <span class="poi-name">{{item.name}}</span> </h3> <div class="poi-info"> <p class="poi-addr">地址:{{item.address}}</p> <p class="poi-tel">电话:{{item.tel}}</p> </div> <div class="clear"></div> </div> </div> </div> </div> <div id="iCenter"></div> <button class="btn" @click="fetAddressName">获取当前位置和名字</button> </div> </template>
js部分
<script> export default { props:['newAddress','dataObj'],// 父组件传过来的地址和地址经纬度信息, data() { return { address:this.newAddress "",//地图对象 selectedIndex: -1, hide: false, clickType: 1, location:{ P:this.dataObj.lat, Q:this.dataObj.lng, } }; }, mounted() { console.log(333,this.dataObj,this.location) this.mapInit() this.placeSearch(this.address) }, methods: { showToast(address){ this.placeSearch(address.address) console.log(444,address) this.location.P =address.lat this.location.Q =address.lng this.address = address.address let that = this; new AMap.InfoWindow({ content:"<h3>" + '当前选中地址' + "</h3>" + that.address, size: new AMap.Size(300, 0), autoMove: true, offset: new AMap.Pixel(-4, -10) }).open(that.mapObj,that.location) }, cancelSave(){ eventBus.$emit('cancelSave') }, saveAddress(){ let addressName,location; if(this.clickType==1){ let address = this.poiArr[this.selectedIndex] addressName = address.name+address.address; location = address.location console.log(address.name+address.address,address.location) }else if(this.clickType==2){ console.log(this.address,this.map) addressName = this.address; location = this.map; }else if(this.clickType==3){ console.log(this.address,this.map1) addressName = this.address; location = this.map1; } eventBus.$emit('saveAddress',[addressName,location]) }, // 经纬度转化为详细地址 getAddress(){ let that = this; AMap.plugin('AMap.Geocoder',function(){ let geocoder = new AMap.Geocoder({ radius: 100, extensions: "all" }); geocoder.getAddress([that.map1.lng,that.map1.lat], function(status, result) { if (status === 'complete' && result.info === 'OK') { let address = result.regeocode.formattedAddress; console.log(result.regeocode); that.address = result.regeocode.formattedAddress; // that.placeSearch(that.address) } }); }) }, // 地图点击事件 testevent(){ let that = this; this.clickType = 3 // var map=new AMap.Map('iCenter');//重新new出一个对象,传入参数是div的id AMap.event.addListener(this.mapObj,'click',function (e) { //添加点击事件,传入对象名,事件名,回调函数 that.map1 = e.lnglat; that.getAddress(); setTimeout(()=>{ new AMap.InfoWindow({ content:"<h3>" + '当前选中地址' + "</h3>" + that.address, size: new AMap.Size(300, 0), autoMove: true, offset: new AMap.Pixel(-4, -10) }).open(that.mapObj,e.lnglat) },100) }) }, //创建一个map mapInit() { this.mapObj = new AMap.Map("iCenter", { resizeEnable: true, zoom: 10, }) this.testevent(); }, //根据名字地址去搜索结果 placeSearch(name) { let that = this; this.hide = true var MSearch; this.mapObj.plugin( ["AMap.PlaceSearch", "AMap.ToolBar", "AMap.Scale"], () => { this.mapObj.addControl(new AMap.ToolBar()) this.mapObj.addControl(new AMap.Scale()) MSearch = new AMap.PlaceSearch({ //构造地点查询类 city: that.address //城市 }); AMap.event.addListener(MSearch,"complete",this.keywordSearch_CallBack) //返回地点查询结果 MSearch.search(name); //关键字查询 } ); }, //结果的回调 keywordSearch_CallBack(data) { console.log(111,data) var poiArr = data.poiList.pois var resultCount = poiArr.length this.poiArr = poiArr; //左边要渲染的数据 for (var i = 0; i < resultCount; i++) { this.addmarker(i, poiArr[i]) console.log(poiArr[i]) this.poiArr[i].url = this.poiArr[i].photos"": "" } this.mapObj.setFitView() }, //添加marker&infowindow addmarker(i, d) { var lngX = d.location.getLng(); var latY = d.location.getLat(); console.log(lngX,latY) var markerOption = { map: this.mapObj, position: new AMap.LngLat(lngX, latY) }; var mar = new AMap.Marker(markerOption); this.marker.push(new AMap.LngLat(lngX, latY)); var infoWindow = new AMap.InfoWindow({ content: "<h3>" +'当前选中位置:'+ d.name + "</h3>" + this.TipContents(d.name, d.address), size: new AMap.Size(300, 0), autoMove: true, offset: new AMap.Pixel(0, -30) }); console.log() this.windowsArr.push(infoWindow); var _this = this; var aa = (e) => { this.clickType = 2 var obj = mar.getPosition(); this.map = obj //这里保存的地址经纬度 this.address = d.name + d.address //这里保存的是地址名字 infoWindow.open(_this.mapObj, obj); } AMap.event.addListener(mar, "click", aa) }, TipContents(name, address) { //窗体内容 if ( name == "" || name == "undefined" || name == null || name == " undefined" || typeof name == "undefined" ) { type = "暂无"; } if ( address == "" || address == "undefined" || address == null || address == " undefined" || typeof address == "undefined" ) { address = "暂无"; } var str = `地址:${address}` return str }, openMarkerTipById(pointid, event) { //根据id 打开搜索结果点tip this.clickType = 1 event.currentTarget.style.background = "#CAE1FF"; this.selectedIndex = pointid // this.map = this.marker[pointid] this.map1 = this.poiArr[pointid].location console.log(222,this.mapObj, this.marker[pointid]) console.log(this.marker[pointid],this.poiArr[pointid]) this.address = this.poiArr[pointid].address + this.poiArr[pointid].name this.windowsArr[pointid].open(this.mapObj, this.marker[pointid]) }, onmouseout_MarkerStyle(pointid, event) { //鼠标移开后点样式恢复 event.currentTarget.style.background = "" }, search() { this.windowsArr = [] this.marker = [] this.mapObj='' this.mapInit() this.placeSearch(this.address) } }, }; </script>
css部分
<style lang="scss"> #wrap{ width:100%; display: flex; #iCenter { height: 600px; position: relative; display: flex; flex: 1; } #searchWrap{ width:300px; position: relative; height:600px; .searchWrap{ position: absolute; width:300px; z-index: 9; display: flex; align-items: center; input{ width:260px; height:24px; } button{ width:36px; height:28px; } } #result { width: 300px; position: absolute; top:30px; height: 570px; z-index: 8; overflow-y: scroll; border-right: 1px solid #ccc; } } .amap_lib_placeSearch { height: 100%; overflow-y: scroll; .poibox { border-bottom: 1px solid #eaeaea; cursor: pointer; padding: 5px 0 5px 10px; position: relative; min-height: 35px; .selected { background-image: url(https://webapi.amap.com/theme/v1.3/markers/n/mark_r.png) !important; } &:hover { background: #f6f6f6; } .poi-img { float: right; margin: 3px 8px 0; width: 90px; height: 56px; overflow: hidden; } .poi-title { margin-left: 25px; font-size: 13px; overflow: hidden; } .poi-info { word-break: break-all; margin: 0 0 0 25px; overflow: hidden; p { color: #999; font-family: Tahoma; line-height: 20px; font-size: 12px; } } .poibox-icon { margin-left: 7px; margin-top: 4px; } .amap_lib_placeSearch_poi { background: url(https://webapi.amap.com/theme/v1.3/markers/n/mark_b.png) no-repeat; height: 31px; width: 19px; cursor: pointer; left: -1px; text-align: center; color: #fff; font: 12px arial, simsun, sans-serif; padding-top: 3px; position: absolute; } } } .btn{ position: fixed; bottom:20px; left:50%; padding:10px; } } </style>
补充知识:vue-amap 高德地图定位 点击获取经纬度和具体地址的使用
官方文档地址: 点这里!!
经纬度获取只要通过点击事件就可以通过e.lnglat来获取,然后就是插件Geocoder使用了。在main.js中initAMapApiLoader中写入:AMap.Geocoder,注意 官方文档中有提示:
所以插件中使用的依然是AMap,与导入名无关
不然会报错,Geocoder无法使用。
定位文档 照着文档写就行 注意在main.js中注册AMap.Geolocation插件,
另外使用到地图的.vue页面 不能使用scoped对样式进行局域化。
以上这篇vue+高德地图实现地图搜索及点击定位操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。
更新日志
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]