1.首先去高德地圖開放平台注冊應用拿到key
2.安裝 npm install vue-amap --save (不知道的去這里查看:https://elemefe.github.io/vue-amap/#/zh-cn/introduction/install)
3.在項目的main.js里面添加:
import VueAMap from 'vue-amap' Vue.use(VueAMap) VueAMap.initAMapApiLoader({ key: '第一步申請的key', plugin: ['AMap.Scale', 'AMap.OverView', 'AMap.ToolBar', 'AMap.MapType', 'AMap.PlaceSearch', 'AMap.Geolocation', 'AMap.Geocoder'], v: '1.4.4', uiVersion: '1.0' })
4.在地圖要顯示的.vue頁面添加:
<template lang="html"> <div style="width:100%;height:800px;"> <div class="container"> <div class="search-box"> <input v-model="searchKey" type="search" id="search"> <button @click="searchByHand">搜索</button> <div class="tip-box" id="searchTip"></div> </div> <!-- amap-manager: 地圖管理對象 vid:地圖容器節點的ID zooms: 地圖顯示的縮放級別范圍,在PC上,默認范圍[3,18],取值范圍[3-18];在移動設備上,默認范圍[3-19],取值范圍[3-19] center: 地圖中心點坐標值 plugin:地圖使用的插件 events: 事件 --> <el-amap class="amap-box" :amap-manager="amapManager" :vid="'amap-vue'" :zoom="zoom" :plugin="plugin" :center="center" :events="events" > <!-- 標記 --> <el-amap-marker v-for="(marker, index) in markers" :position="marker" :key="index"></el-amap-marker> </el-amap> </div> </div> </template> <script> import { AMapManager, lazyAMapApiLoaderInstance } from 'vue-amap' let amapManager = new AMapManager() export default { data() { let self = this return { address: null, searchKey: '', amapManager, markers: [], searchOption: { city: '全國', citylimit: true }, center: [121.329402, 31.228667], zoom: 17, lng: 0, lat: 0, loaded: false, events: { init() { lazyAMapApiLoaderInstance.load().then(() => { self.initSearch() }) }, // 點擊獲取地址的數據 click(e) { // console.log(e) self.markers = [] let { lng, lat } = e.lnglat self.lng = lng self.lat = lat self.center = [lng, lat] self.markers.push([lng, lat]) // 這里通過高德 SDK 完成。 let geocoder = new AMap.Geocoder({ radius: 1000, extensions: 'all' }) geocoder.getAddress([lng, lat], function(status, result) { if (status === 'complete' && result.info === 'OK') { if (result && result.regeocode) { console.log(result.regeocode.formattedAddress) self.address = result.regeocode.formattedAddress self.searchKey = result.regeocode.formattedAddress self.$nextTick() } } }) } }, // 一些工具插件 plugin: [ { pName: 'Geocoder', events: { init (o) { console.log(o.getAddress()) } } }, { // 定位 pName: 'Geolocation', events: { init(o) { // o是高德地圖定位插件實例 o.getCurrentPosition((status, result) => { if (result && result.position) { // 設置經度 self.lng = result.position.lng // 設置維度 self.lat = result.position.lat // 設置坐標 self.center = [self.lng, self.lat] self.markers.push([self.lng, self.lat]) // load self.loaded = true // 頁面渲染好后 self.$nextTick() } }) } } }, { // 工具欄 pName: 'ToolBar', events: { init(instance) { // console.log(instance); } } }, { // 鷹眼 pName: 'OverView', events: { init(instance) { // console.log(instance); } } }, { // 地圖類型 pName: 'MapType', defaultType: 0, events: { init(instance) { // console.log(instance); } } }, { // 搜索 pName: 'PlaceSearch', events: { init(instance) { // console.log(instance) } } } ] } }, methods: { initSearch() { let vm = this let map = this.amapManager.getMap() AMapUI.loadUI(['misc/PoiPicker'], function(PoiPicker) { var poiPicker = new PoiPicker({ input: 'search', placeSearchOptions: { map: map, pageSize: 10 }, suggestContainer: 'searchTip', searchResultsContainer: 'searchTip' }) vm.poiPicker = poiPicker // 監聽poi選中信息 poiPicker.on('poiPicked', function(poiResult) { // console.log(poiResult) let source = poiResult.source let poi = poiResult.item if (source !== 'search') { poiPicker.searchByKeyword(poi.name) } else { poiPicker.clearSearchResults() vm.markers = [] let lng = poi.location.lng let lat = poi.location.lat let address = poi.cityname + poi.adname + poi.name vm.center = [lng, lat] vm.markers.push([lng, lat]) vm.lng = lng vm.lat = lat vm.address = address vm.searchKey = address } }) }) }, searchByHand() { if (this.searchKey !== '') { this.poiPicker.searchByKeyword(this.searchKey) } } } } </script> <style lang="css"> .container { width: 100%; height: 100%; position: relative; left: 50%; top: 50%; transform: translate3d(-50%, -50%, 0); border: 1px solid #999; } .search-box { position: absolute; z-index: 5; width: 30%; left: 13%; top: 10px; height: 30px; } .search-box input { float: left; width: 80%; height: 100%; border: 1px solid #30ccc1; padding: 0 8px; outline: none; } .search-box button { float: left; width: 20%; height: 100%; background: #30ccc1; border: 1px solid #30ccc1; color: #fff; outline: none; } .tip-box { width: 100%; max-height: 260px; position: absolute; top: 30px; overflow-y: auto; background-color: #fff; } </style>
5.效果圖: