1. 程式人生 > 其它 >vue2中使用高德地圖實現搜尋定位(一)

vue2中使用高德地圖實現搜尋定位(一)

vue2專案中有用到高德地圖,以此記錄:

需求:搜尋可定位地點

過程:在vue2項執行起地圖,在點圖實現點標記,搜尋功能,切換標記

vue-amap官方文件地址:https://github.com/ElemeFE/vue-amap/

安裝 amap:

npm install vue-amap --save

地圖搭建:

main.js

import VueAMap from "vue-amap"
Vue.use(VueAMap)
VueAMap.initAMapApiLoader({
  key: "xxxxxxxxxxxxxxx",
  plugin: [
    "AMap.Autocomplete", //輸入提示外掛
    "AMap.PlaceSearch", //POI搜尋外掛
    "AMap.Scale", //右下角縮圖外掛 比例尺
    "AMap.OverView", //地圖鷹眼外掛
    "AMap.ToolBar", //地圖工具條
    "AMap.Geolocation", //定位控制元件,用來獲取和展示使用者主機所在的經緯度位置
    "AMap.Geocoder"// 逆地理編碼,通過經緯度獲取地址所在位置詳細資訊
    // 根據需求選用
  ],
  uiVersion: "1.0", // 地圖ui版本
  v : '1.4.4' // amap版本
})

vue檔案:

<template>
  <div class="contain">
    <el-amap-search-box class="search-box" :search-option="searchOption" :on-search-result="onSearchResult">
    </el-amap-search-box>
    <el-amap ref="map" vid="amapDemo" :center="center" :zoom="zoom" class="amap-demo">
      <el-amap-marker :position="center" key="100"></el-amap-marker>
    </el-amap>
    <div v-if="searchResult">
      搜尋:{{content}},詳細地址為:{{searchResult.locationName}},經度:{{searchResult.longitude}},緯度:{{searchResult.latitude}}
    </div>
  </div>
</template>

<script>
  export default {
    name: 'AMapDemo',
    data() {
      return {
        zoom: 12,
        center: [118.02, 24.48],
        searchOption: {
          // 限制搜尋城市的範圍
          citylimit: false
        },
        content: '',
        inputValue: '',
        searchResult: {
          address: '',
          latitude: '',
          longitude: '',
          name: '',
          type: '',
          country: '',
          province: '',
          city: '',
          area: '',
          township: '',
          street: '',
          neighborhood: '',
          locationName: ''
        }
      }
    },
    methods: {
      onSearchResult(pois) {
        //搜尋
        console.log(pois)
        let latSum = 0
        let lngSum = 0
        let that = this
        if (pois && pois.length > 0) {
          //如果長度為1則無需轉化
          let poi = pois[0]
          let lng = poi["lng"]
          let lat = poi["lat"]
          that.center = [lng, lat]
          that.zoom = 13
          that.content = poi.name
          console.log(poi.name)
          that.searchResult.address = poi.address
          that.searchResult.latitude = poi.lat
          that.searchResult.longitude = poi.lng
          let geocoder = new AMap.Geocoder({})
          geocoder.getAddress(that.center, function(status, result) {
            console.log(result)
            if (status === 'complete' && result.info === 'OK') {
              let obj = result.regeocode.addressComponent
              that.searchResult.locationName = obj.province + obj.city + obj.district + obj.township + obj.street +
                poi.address
            }
          });

        } else {
          that.searchResult = []
        }
      },
    }
  }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss" scoped>
  .amap-demo {
    width: 100%;
    height: 600px;
    position: relative;
  }

  .search-box {
    height: 35px;
    margin: 10px auto;
    width: calc(100% - 20px);
    // border-radius:16px;
    box-shadow: none;
    background: #ffff;
    border: 1px solid #e6e6e6;

    .search-box-wrapper {
      input {
        background: #fff;
        padding-left: 20px;
      }

      .search-btn {
        color: #2A67FE;
        width: 90px;
        height: 20px;
        box-sizing: border-box;
        border-left: 1px solid #D7D7D7;
      }
    }
  }
</style>

 最終效果如下:

自此,地圖部署完成也可以搜尋,但是想點地圖上的點,並且獲取經緯度並未實現,