首先在index.html引入高德地圖的jscss
<script src="https://webapi.amap.com/maps?v=1.4.14&key=你的key"></script>
而後寫html部分html
<template> <div class="mymapM"> <!--捜索--> <div v-if="loading" class="loading"> <van-loading type="spinner" /> </div> <div class="search-box"> <div class="search-postion"> <span class="buts">返回</span> <input type="text" placeholder="輸入關鍵字搜索" v-model="search_key" /> <span class="clear" v-if="search_key" @click="search_key ='' "> <van-icon color="#8f8f8f" name="clear" /> </span> <span class="buts border_but" @click="keySearch()">捜索</span> </div> </div> <div class="con-box con-map" v-if="!search_key"> <!--地圖--> <div class="mapbox"> <div class="map" id="container"></div> <div class="sign"></div> </div> </div> <div class="con-box" v-if="!search_key"> <!--地址列表--> <div class="Hlist-box"> <ul> <li v-for="(item, index) in lists" :key="index" @click="onAddressLi(item)"> <b> <van-icon color="#a6a6a6" name="clock" /> </b> <div> <span>{{item.name}}</span> <p>{{item.address}}</p> </div> </li> </ul> </div> </div> <!--捜索列表--> <div class="search-list" v-if="search_key"> <ul> <li v-for="(item, index) in search_list" :key="index" @click="onSearchLi(item.location)"> <span>{{item.name}}</span> <p>{{item.address}}</p> </li> <li v-if="noSearchShow"> <p>暫無搜索結果</p> </li> </ul> </div> </div> </template>
css部分web
<style lang="scss" scoped> .con-map { height: 190px; width: 100%; } .con-box { .mapbox { margin-bottom: 10px; position: fixed; z-index: 111; width: 100%; height: 180px; padding: 10px 0; background: #eceeee; .map { width: 100%; height: 180px; } } .Hlist-box { width: 96%; margin: 0 auto; background: #fff; border-radius: 5px; li { height: 40px; padding: 14px 22px; border-bottom: 1px solid #d9d9d9; display: flex; b { display: inline-block; i { margin: 18px 18px 0 0; } } div { width: 100%; } span { white-space: nowrap; text-overflow: ellipsis; overflow: hidden; font-size: 15px; display: inline-block; width: 90%; } p { margin-top: 10px; color: #a6a6a6; white-space: nowrap; text-overflow: ellipsis; overflow: hidden; font-size: 13px; width: 90%; } } } } .mymapM { .search-box { height: 48px; line-height: 48px; background: #fff; border-bottom: 1px solid #bfbec4; .search-postion { height: 48px; line-height: 48px; background: #fff; border-bottom: 1px solid #bfbec4; width: 100%; position: fixed; z-index: 99999; display: flex; input { flex: 4; height: 14px; padding: 16px 0; border: none; text-indent: 10px; } .clear { margin: 2px 6px; } .buts { width: 15%; text-align: center; display: inline-block; flex: 1; } .border_but { border-left: 1px solid #8f8f8f; height: 14px; line-height: 14px; margin: 17px 0; } } } .search-list { width: 96%; margin: 0 auto; margin-top: 10px; border-radius: 5px; background: #fff; li { height: 40px; padding: 14px 22px; border-bottom: 1px solid #d9d9d9; span { white-space: nowrap; text-overflow: ellipsis; overflow: hidden; font-size: 15px; display: inline-block; width: 90%; } p { margin-top: 10px; color: #a6a6a6; white-space: nowrap; text-overflow: ellipsis; overflow: hidden; font-size: 13px; width: 90%; } } } } .loading { position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); z-index: 99999999; } </style>
js部分api
<script> export default { data() { return { center: [116.42792, 39.902896], //經度+緯度 search_key: "", //搜索值 lists: [], //地點列表 search_list: [], //搜索結果列表 marker: "", loading: false, noSearchShow: false //無搜索結果提示,無搜索結果時會顯示暫無搜索結果 }; }, mounted() { setTimeout(() => { this.adMap(); }, 1000); }, methods: { adMap() { this.loading = true; //初始化地圖 var map = new AMap.Map("container", { zoom: 14, //縮放級別 center: this.center //設置地圖中心點 //resizeEnable: true, //地圖初始化加載定位到當前城市 }); //獲取初始中心點並賦值 var currentCenter = map.getCenter(); //此方法是獲取當前地圖的中心點 this.center = [currentCenter.lng, currentCenter.lat]; //將獲取到的中心點的緯度經度賦值給data的center console.log(this.center); //建立標記 this.marker = new AMap.Marker({ position: new AMap.LngLat(currentCenter.lng, currentCenter.lat) // 經緯度對象,也能夠是經緯度構成的一維數組[116.39, 39.9] }); // 將建立的點標記添加到已有的地圖實例: map.add(this.marker); //根據地圖中心點查附近地點,此方法在下方 this.centerSearch(); //監聽地圖移動事件,並在移動結束後獲取地圖中心點並更新地點列表 var moveendFun = e => { // 獲取地圖中心點 currentCenter = map.getCenter(); this.center = [currentCenter.lng, currentCenter.lat]; this.marker.setPosition([currentCenter.lng, currentCenter.lat]); //更新標記的位置 //根據地圖中心點查附近地點 }; //更新數據 var centerSearch = () => { this.loading = true; this.centerSearch(); }; // 綁定事件移動地圖事件 map.on("mapmove", moveendFun); //更新標記 map.on("moveend", centerSearch); //更新數據 }, centerSearch() { AMap.service(["AMap.PlaceSearch"], () => { //構造地點查詢類 var placeSearch = new AMap.PlaceSearch({ type: "汽車服務|餐飲服務|購物服務|生活服務|體育休閒服務|醫療保健服務|住宿服務|風景名勝|商務住宅|政府機構及社會團體|科教文化服務|交通設施服務|金融保險服務|公司企業|地名地址信息", // 興趣點類別 pageSize: 30, // 單頁顯示結果條數 pageIndex: 1, // 頁碼 city: "全國", // 興趣點城市 autoFitView: false // 是否自動調整地圖視野使繪製的 Marker點都處於視口的可見範圍 }); //根據地圖中心點查附近地點 placeSearch.searchNearBy( "", [this.center[0], this.center[1]], 200, (status, result) => { if (status == "complete") { this.lists = result.poiList.pois; //將查詢到的地點賦值 this.loading = false; } } ); }); }, keySearch() { this.loading = true; AMap.service(["AMap.PlaceSearch"], () => { //構造地點查詢類 var placeSearch = new AMap.PlaceSearch({ type: "汽車服務|餐飲服務|購物服務|生活服務|體育休閒服務|醫療保健服務|住宿服務|風景名勝|商務住宅|政府機構及社會團體|科教文化服務|交通設施服務|金融保險服務|公司企業|地名地址信息", // 興趣點類別 pageSize: 30, // 單頁顯示結果條數 pageIndex: 1, // 頁碼 city: "全國", // 興趣點城市 citylimit: false, //是否強制限制在設置的城市內搜索 autoFitView: false // 是否自動調整地圖視野使繪製的 Marker點都處於視口的可見範圍 }); //關鍵字查詢 placeSearch.search(this.search_key, (status, result) => { if (status == "complete") { if (result.poiList.count === 0) { this.noSearchShow = true; } else { this.search_list = result.poiList.pois; //將查詢到的地點賦值 this.noSearchShow = false; this.loading = false; } } else { this.search_list = []; this.noSearchShow = true; } }); }); }, onAddressLi(e) { console.log(e); this.marker.setPosition([e.location.lng, e.location.lat]); //更新標記的位置 }, onSearchLi(e) { console.log(e.lng + "-" + e.lat); this.center = [e.lng, e.lat]; console.log(this.center); this.search_key = ""; // this.loading=true; setTimeout(() => { this.adMap(); }, 1000); } }, watch: { search_key(newv, oldv) { if (newv == "") { this.search_list = []; this.noSearchShow = false; setTimeout(() => { this.adMap(); }, 1000); } } } }; </script>
效果圖數組