一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專(zhuān)注于服務(wù)器技術(shù)及軟件下載分享
分類(lèi)導(dǎo)航

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

服務(wù)器之家 - 編程語(yǔ)言 - JavaScript - vue+高德地圖實(shí)現(xiàn)地圖搜索及點(diǎn)擊定位操作

vue+高德地圖實(shí)現(xiàn)地圖搜索及點(diǎn)擊定位操作

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

這篇文章主要介紹了vue+高德地圖實(shí)現(xiàn)地圖搜索及點(diǎn)擊定位操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

首先需要在index.html中引入高德地圖的js鏈接,key需要換成你自己的key

最近有個(gè)需求是實(shí)現(xiàn)一個(gè)使用地圖搜索定位的功能,在網(wǎng)上參考了下其他的文章,感覺(jué)不是很完善,自己整理了一下,可以實(shí)現(xiàn)點(diǎn)擊定位,搜索列表定位等功能,可能有些地方是多余的,需要的自己看著改下

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

效果圖如下

vue+高德地圖實(shí)現(xiàn)地圖搜索及點(diǎn)擊定位操作

下邊就是實(shí)現(xiàn)過(guò)程

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">電話(huà):{{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">獲取當(dāng)前位置和名字</button>
  30. </div>
  31. </template>

js部分

  1. <script>
  2. export default {
  3. props:['newAddress','dataObj'],// 父組件傳過(guò)來(lái)的地址和地址經(jīng)緯度信息,
  4. data() {
  5. return {
  6. address:this.newAddress ? this.newAddress : '鄭州',//保存地址的漢字名字
  7. map1: '',
  8. map:'',//保存地址的經(jīng)緯度
  9. poiArr: [],//左邊搜索出來(lái)的數(shù)組
  10. windowsArr: [],//信息窗口的數(shù)組
  11. marker: [],
  12. mapObj: "",//地圖對(duì)象
  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>" + '當(dāng)前選中地址' + "</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. // 經(jīng)緯度轉(zhuǎn)化為詳細(xì)地址
  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. // 地圖點(diǎn)擊事件
  84. testevent(){
  85. let that = this;
  86. this.clickType = 3
  87. // var map=new AMap.Map('iCenter');//重新new出一個(gè)對(duì)象,傳入?yún)?shù)是div的id
  88. AMap.event.addListener(this.mapObj,'click',function (e) { //添加點(diǎn)擊事件,傳入對(duì)象名,事件名,回調(diào)函數(shù)
  89. that.map1 = e.lnglat;
  90. that.getAddress();
  91. setTimeout(()=>{
  92. new AMap.InfoWindow({
  93. content:"<h3>" + '當(dāng)前選中地址' + "</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. //創(chuàng)建一個(gè)map
  102. mapInit() {
  103. this.mapObj = new AMap.Map("iCenter", {
  104. resizeEnable: true,
  105. zoom: 10,
  106. })
  107. this.testevent();
  108. },
  109. //根據(jù)名字地址去搜索結(jié)果
  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. //構(gòu)造地點(diǎn)查詢(xún)類(lèi)
  121. city: that.address //城市
  122. });
  123. AMap.event.addListener(MSearch,"complete",this.keywordSearch_CallBack) //返回地點(diǎn)查詢(xún)結(jié)果
  124. MSearch.search(name); //關(guān)鍵字查詢(xún)
  125. }
  126. );
  127. },
  128. //結(jié)果的回調(diào)
  129. keywordSearch_CallBack(data) {
  130. console.log(111,data)
  131. var poiArr = data.poiList.pois
  132. var resultCount = poiArr.length
  133. this.poiArr = poiArr; //左邊要渲染的數(shù)據(jù)
  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āng)前選中位置:'+ 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 //這里保存的地址經(jīng)緯度
  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. //窗體內(nèi)容
  172. if (
  173. name == "" ||
  174. name == "undefined" ||
  175. name == null ||
  176. name == " undefined" ||
  177. typeof name == "undefined"
  178. ) {
  179. type = "暫無(wú)";
  180. }
  181. if (
  182. address == "" ||
  183. address == "undefined" ||
  184. address == null ||
  185. address == " undefined" ||
  186. typeof address == "undefined"
  187. ) {
  188. address = "暫無(wú)";
  189. }
  190. var str = `地址:${address}`
  191. return str
  192. },
  193. openMarkerTipById(pointid, event) {
  194. //根據(jù)id 打開(kāi)搜索結(jié)果點(diǎn)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. //鼠標(biāo)移開(kāi)后點(diǎn)樣式恢復(fù)
  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>

補(bǔ)充知識(shí):vue-amap 高德地圖定位 點(diǎn)擊獲取經(jīng)緯度和具體地址的使用

官方文檔地址: 點(diǎn)這里!!

vue+高德地圖實(shí)現(xiàn)地圖搜索及點(diǎn)擊定位操作

經(jīng)緯度獲取只要通過(guò)點(diǎn)擊事件就可以通過(guò)e.lnglat來(lái)獲取,然后就是插件Geocoder使用了。在main.js中initAMapApiLoader中寫(xiě)入:AMap.Geocoder,注意 官方文檔中有提示:

vue+高德地圖實(shí)現(xiàn)地圖搜索及點(diǎn)擊定位操作

所以插件中使用的依然是AMap,與導(dǎo)入名無(wú)關(guān)

vue+高德地圖實(shí)現(xiàn)地圖搜索及點(diǎn)擊定位操作

不然會(huì)報(bào)錯(cuò),Geocoder無(wú)法使用。

定位文檔 照著文檔寫(xiě)就行 注意在main.js中注冊(cè)AMap.Geolocation插件,

另外使用到地圖的.vue頁(yè)面 不能使用scoped對(duì)樣式進(jìn)行局域化。

以上這篇vue+高德地圖實(shí)現(xiàn)地圖搜索及點(diǎn)擊定位操作就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持我們。

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲第一se情网站 | 亚洲免费在线视频 | 福利久草 | 久久人妻少妇嫩草AV無碼 | 欧美又大又粗又长又硬 | 好大好硬好湿好紧h | 久久婷婷五月综合色丁香 | 欧美成人免费观看bbb | 男人含玉势出嫁束器 | 91桃花 | 色综七七久久成人影 | 亚洲精品中文字幕第一区 | 欧美福利二区 | 亚洲va久久久久 | 男女肉文高h | 夫承子液by免费阅读 | 精品小视频在线观看 | 男女视频在线观看 | 亚洲国产第一区二区香蕉日日 | 成人网欧美亚洲影视图片 | 亚洲性综合网 | 精品视频在线免费观看 | 精品视频免费在线观看 | 国产51页| 91色+91sesex| 国产午夜亚洲精品理论片不卡 | 国产精品久久久久久久久免费 | 成人观看免费观看视频 | 国产愉拍精品视频手机 | 青草青草久热精品视频在线网站 | 久久热这里只有 精品 | 国产综合图区 | 国产欧美视频在线观看 | 丝袜护士强制脚足取精 | 日本人在线看片 | 第一次破女视频国产一级 | 欧洲另类一二三四区 | 无限在线看免费视频大全 | 处女私拍 | 欧美无专区| 国产福利自产拍在线观看 |