服务器之家:专注于服务器技术及软件下载分享
分类导航

node.js|vue.js|jquery|angularjs|React|json|js教程|

服务器之家 - 编程语言 - JavaScript - vue+高德地图实现地图搜索及点击定位操作

vue+高德地图实现地图搜索及点击定位操作

2021-09-23 16:24小_辉 JavaScript

这篇文章主要介绍了vue+高德地图实现地图搜索及点击定位操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

首先需要在index.html中引入高德地图的js链接,key需要换成你自己的key

最近有个需求是实现一个使用地图搜索定位的功能,在网上参考了下其他的文章,感觉不是很完善,自己整理了一下,可以实现点击定位,搜索列表定位等功能,可能有些地方是多余的,需要的自己看着改下

<script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.14&key=你的key"></script>

效果图如下

vue+高德地图实现地图搜索及点击定位操作

下边就是实现过程

html部分

  1. <template>
  2. <div id="wrap">
  3. <div id="searchWrap">
  4. <div class="searchWrap">
  5. <input type="text" v-model="address" @input="search"><button @click="search">搜索</button>
  6. </div>
  7. <div id="result" class="amap_lib_placeSearch" v-show="hide">
  8. <div class="amap_lib_placeSearch_list amap-pl-pc" v-for="(item,index) in poiArr"
  9. @click="openMarkerTipById(index,$event)"
  10. @mouseout="onmouseout_MarkerStyle(index+1,$event)"
  11. :key="index">
  12. <div class="poibox" style="border-bottom: 1px solid #eaeaea">
  13. <div class="amap_lib_placeSearch_poi poibox-icon" :class="index==selectedIndex?'selected':''">{{index+1}}</div>
  14. <div class="poi-img" v-if="item.url" :style="'background-image:url('+item.url+'?operate=merge&amp;w=90&amp;h=56&amp;position=5)'"
  15. ></div>
  16. <h3 class="poi-title" >
  17. <span class="poi-name">{{item.name}}</span>
  18. </h3>
  19. <div class="poi-info">
  20. <p class="poi-addr">地址:{{item.address}}</p>
  21. <p class="poi-tel">电话:{{item.tel}}</p>
  22. </div>
  23. <div class="clear"></div>
  24. </div>
  25. </div>
  26. </div>
  27. </div>
  28. <div id="iCenter"></div>
  29. <button class="btn" @click="fetAddressName">获取当前位置和名字</button>
  30. </div>
  31. </template>

js部分

  1. <script>
  2. export default {
  3. props:['newAddress','dataObj'],// 父组件传过来的地址和地址经纬度信息,
  4. data() {
  5. return {
  6. address:this.newAddress ? this.newAddress : '郑州',//保存地址的汉字名字
  7. map1: '',
  8. map:'',//保存地址的经纬度
  9. poiArr: [],//左边搜索出来的数组
  10. windowsArr: [],//信息窗口的数组
  11. marker: [],
  12. mapObj: "",//地图对象
  13. selectedIndex: -1,
  14. hide: false,
  15. clickType: 1,
  16. location:{
  17. P:this.dataObj.lat,
  18. Q:this.dataObj.lng,
  19. }
  20. };
  21. },
  22. mounted() {
  23. console.log(333,this.dataObj,this.location)
  24. this.mapInit()
  25. this.placeSearch(this.address)
  26.  
  27. },
  28. methods: {
  29. showToast(address){
  30. this.placeSearch(address.address)
  31. console.log(444,address)
  32. this.location.P =address.lat
  33. this.location.Q =address.lng
  34. this.address = address.address
  35. let that = this;
  36. new AMap.InfoWindow({
  37. content:"<h3>" + '当前选中地址' + "</h3>" + that.address,
  38. size: new AMap.Size(300, 0),
  39. autoMove: true,
  40. offset: new AMap.Pixel(-4, -10)
  41. }).open(that.mapObj,that.location)
  42. },
  43. cancelSave(){
  44. eventBus.$emit('cancelSave')
  45. },
  46. saveAddress(){
  47. let addressName,location;
  48. if(this.clickType==1){
  49. let address = this.poiArr[this.selectedIndex]
  50. addressName = address.name+address.address;
  51. location = address.location
  52. console.log(address.name+address.address,address.location)
  53.  
  54. }else if(this.clickType==2){
  55. console.log(this.address,this.map)
  56. addressName = this.address;
  57. location = this.map;
  58. }else if(this.clickType==3){
  59. console.log(this.address,this.map1)
  60. addressName = this.address;
  61. location = this.map1;
  62. }
  63. eventBus.$emit('saveAddress',[addressName,location])
  64. },
  65. // 经纬度转化为详细地址
  66. getAddress(){
  67. let that = this;
  68. AMap.plugin('AMap.Geocoder',function(){
  69. let geocoder = new AMap.Geocoder({
  70. radius: 100,
  71. extensions: "all"
  72. });
  73. geocoder.getAddress([that.map1.lng,that.map1.lat], function(status, result) {
  74. if (status === 'complete' && result.info === 'OK') {
  75. let address = result.regeocode.formattedAddress;
  76. console.log(result.regeocode);
  77. that.address = result.regeocode.formattedAddress;
  78. // that.placeSearch(that.address)
  79. }
  80. });
  81. })
  82. },
  83. // 地图点击事件
  84. testevent(){
  85. let that = this;
  86. this.clickType = 3
  87. // var map=new AMap.Map('iCenter');//重新new出一个对象,传入参数是div的id
  88. AMap.event.addListener(this.mapObj,'click',function (e) { //添加点击事件,传入对象名,事件名,回调函数
  89. that.map1 = e.lnglat;
  90. that.getAddress();
  91. setTimeout(()=>{
  92. new AMap.InfoWindow({
  93. content:"<h3>" + '当前选中地址' + "</h3>" + that.address,
  94. size: new AMap.Size(300, 0),
  95. autoMove: true,
  96. offset: new AMap.Pixel(-4, -10)
  97. }).open(that.mapObj,e.lnglat)
  98. },100)
  99. })
  100. },
  101. //创建一个map
  102. mapInit() {
  103. this.mapObj = new AMap.Map("iCenter", {
  104. resizeEnable: true,
  105. zoom: 10,
  106. })
  107. this.testevent();
  108. },
  109. //根据名字地址去搜索结果
  110. placeSearch(name) {
  111. let that = this;
  112. this.hide = true
  113. var MSearch;
  114. this.mapObj.plugin(
  115. ["AMap.PlaceSearch", "AMap.ToolBar", "AMap.Scale"],
  116. () => {
  117. this.mapObj.addControl(new AMap.ToolBar())
  118. this.mapObj.addControl(new AMap.Scale())
  119. MSearch = new AMap.PlaceSearch({
  120. //构造地点查询类
  121. city: that.address //城市
  122. });
  123. AMap.event.addListener(MSearch,"complete",this.keywordSearch_CallBack) //返回地点查询结果
  124. MSearch.search(name); //关键字查询
  125. }
  126. );
  127. },
  128. //结果的回调
  129. keywordSearch_CallBack(data) {
  130. console.log(111,data)
  131. var poiArr = data.poiList.pois
  132. var resultCount = poiArr.length
  133. this.poiArr = poiArr; //左边要渲染的数据
  134. for (var i = 0; i < resultCount; i++) {
  135. this.addmarker(i, poiArr[i])
  136. console.log(poiArr[i])
  137. this.poiArr[i].url = this.poiArr[i].photos? this.poiArr[i].photos[0]? this.poiArr[i].photos[0].url: "": ""
  138. }
  139. this.mapObj.setFitView()
  140. },
  141. //添加marker&infowindow
  142. addmarker(i, d) {
  143. var lngX = d.location.getLng();
  144. var latY = d.location.getLat();
  145. console.log(lngX,latY)
  146. var markerOption = {
  147. map: this.mapObj,
  148. position: new AMap.LngLat(lngX, latY)
  149. };
  150. var mar = new AMap.Marker(markerOption);
  151. this.marker.push(new AMap.LngLat(lngX, latY));
  152. var infoWindow = new AMap.InfoWindow({
  153. content: "<h3>" +'当前选中位置:'+ d.name + "</h3>" + this.TipContents(d.name, d.address),
  154. size: new AMap.Size(300, 0),
  155. autoMove: true,
  156. offset: new AMap.Pixel(0, -30)
  157. });
  158. console.log()
  159. this.windowsArr.push(infoWindow);
  160. var _this = this;
  161. var aa = (e) => {
  162. this.clickType = 2
  163. var obj = mar.getPosition();
  164. this.map = obj //这里保存的地址经纬度
  165. this.address = d.name + d.address //这里保存的是地址名字
  166. infoWindow.open(_this.mapObj, obj);
  167. }
  168. AMap.event.addListener(mar, "click", aa)
  169. },
  170. TipContents(name, address) {
  171. //窗体内容
  172. if (
  173. name == "" ||
  174. name == "undefined" ||
  175. name == null ||
  176. name == " undefined" ||
  177. typeof name == "undefined"
  178. ) {
  179. type = "暂无";
  180. }
  181. if (
  182. address == "" ||
  183. address == "undefined" ||
  184. address == null ||
  185. address == " undefined" ||
  186. typeof address == "undefined"
  187. ) {
  188. address = "暂无";
  189. }
  190. var str = `地址:${address}`
  191. return str
  192. },
  193. openMarkerTipById(pointid, event) {
  194. //根据id 打开搜索结果点tip
  195. this.clickType = 1
  196. event.currentTarget.style.background = "#CAE1FF";
  197. this.selectedIndex = pointid
  198. // this.map = this.marker[pointid]
  199. this.map1 = this.poiArr[pointid].location
  200. console.log(222,this.mapObj, this.marker[pointid])
  201. console.log(this.marker[pointid],this.poiArr[pointid])
  202. this.address = this.poiArr[pointid].address + this.poiArr[pointid].name
  203. this.windowsArr[pointid].open(this.mapObj, this.marker[pointid])
  204.  
  205. },
  206. onmouseout_MarkerStyle(pointid, event) {
  207. //鼠标移开后点样式恢复
  208. event.currentTarget.style.background = ""
  209. },
  210. search() {
  211. this.windowsArr = []
  212. this.marker = []
  213.  
  214. this.mapObj=''
  215. this.mapInit()
  216. this.placeSearch(this.address)
  217. }
  218. },
  219. };
  220. </script>

css部分

  1. <style lang="scss">
  2. #wrap{
  3. width:100%;
  4. display: flex;
  5. #iCenter {
  6. height: 600px;
  7. position: relative;
  8. display: flex;
  9. flex: 1;
  10. }
  11. #searchWrap{
  12. width:300px;
  13. position: relative;
  14. height:600px;
  15. .searchWrap{
  16. position: absolute;
  17. width:300px;
  18. z-index: 9;
  19. display: flex;
  20. align-items: center;
  21. input{
  22. width:260px;
  23. height:24px;
  24. }
  25. button{
  26. width:36px;
  27. height:28px;
  28. }
  29. }
  30. #result {
  31. width: 300px;
  32. position: absolute;
  33. top:30px;
  34. height: 570px;
  35. z-index: 8;
  36. overflow-y: scroll;
  37. border-right: 1px solid #ccc;
  38. }
  39. }
  40. .amap_lib_placeSearch {
  41. height: 100%;
  42. overflow-y: scroll;
  43. .poibox {
  44. border-bottom: 1px solid #eaeaea;
  45. cursor: pointer;
  46. padding: 5px 0 5px 10px;
  47. position: relative;
  48. min-height: 35px;
  49. .selected {
  50. background-image: url(https://webapi.amap.com/theme/v1.3/markers/n/mark_r.png) !important;
  51. }
  52. &:hover {
  53. background: #f6f6f6;
  54. }
  55. .poi-img {
  56. float: right;
  57. margin: 3px 8px 0;
  58. width: 90px;
  59. height: 56px;
  60. overflow: hidden;
  61. }
  62. .poi-title {
  63. margin-left: 25px;
  64. font-size: 13px;
  65. overflow: hidden;
  66. }
  67. .poi-info {
  68. word-break: break-all;
  69. margin: 0 0 0 25px;
  70. overflow: hidden;
  71. p {
  72. color: #999;
  73. font-family: Tahoma;
  74. line-height: 20px;
  75. font-size: 12px;
  76. }
  77. }
  78. .poibox-icon {
  79. margin-left: 7px;
  80. margin-top: 4px;
  81. }
  82. .amap_lib_placeSearch_poi {
  83. background: url(https://webapi.amap.com/theme/v1.3/markers/n/mark_b.png)
  84. no-repeat;
  85. height: 31px;
  86. width: 19px;
  87. cursor: pointer;
  88. left: -1px;
  89. text-align: center;
  90. color: #fff;
  91. font: 12px arial, simsun, sans-serif;
  92. padding-top: 3px;
  93. position: absolute;
  94. }
  95. }
  96. }
  97. .btn{
  98. position: fixed;
  99. bottom:20px;
  100. left:50%;
  101. padding:10px;
  102. }
  103. }
  104. </style>

补充知识:vue-amap 高德地图定位 点击获取经纬度和具体地址的使用

官方文档地址: 点这里!!

vue+高德地图实现地图搜索及点击定位操作

经纬度获取只要通过点击事件就可以通过e.lnglat来获取,然后就是插件Geocoder使用了。在main.js中initAMapApiLoader中写入:AMap.Geocoder,注意 官方文档中有提示:

vue+高德地图实现地图搜索及点击定位操作

所以插件中使用的依然是AMap,与导入名无关

vue+高德地图实现地图搜索及点击定位操作

不然会报错,Geocoder无法使用。

定位文档 照着文档写就行 注意在main.js中注册AMap.Geolocation插件,

另外使用到地图的.vue页面 不能使用scoped对样式进行局域化。

以上这篇vue+高德地图实现地图搜索及点击定位操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。

原文链接:https://blog.csdn.net/qq_42165062/article/details/92782197

延伸 · 阅读

精彩推荐