vue 实现高德地图搜索地址获取经纬度

这是一个使用Vue.js编写的组件,实现了地图搜索和查询功能。用户可以输入地址进行查询,点击地图上的点获取经纬度,并通过输入框展示。组件还提供了清除标记和确认填入经纬度的按钮,支持地图的点击事件和高德地图API集成。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<template>
  <div class="map-search_wrapper" id="bmapSearch">
    <div class="header">
      需要查询的地址:
      <el-tooltip class="item" effect="dark" :content="textTip" placement="top" :value="isshowTip" manual>
        <el-input
          type="text"
          top="-140px"
          placeholder="请输入内容"
          @focus="editText"
          v-model="keyWord"
          @keyup.native.13="search"
          clearable
          class="search-input">
        </el-input>
      </el-tooltip>
      <el-button type="primary" size="medium" @click="search">查询</el-button>
      &nbsp;&nbsp;&nbsp;查询结果(经纬度):
      <el-input
        type="text"
        v-model="longAndLat"
        ref="long"
        class="search-input">
      </el-input>
      <el-button type="primary" size="medium" @click="comfirmBmap">填入</el-button>
      <el-button type="warning" size="medium" @click="closeBmap">关闭</el-button>
    </div>
    <div id="container" style="overflow:hidden;"></div>
  </div>
</template>
<script>
// import { collide } from '../../utils/utils';
import { asyncJS } from 'utils/utils.js';
  export default {
    data () {
      return {
        keyWord: '',
        longAndLat: '',
        map: null,
        mapMark: null,
        localSearch: '',
        textTip: '',
        isshowTip: false
      };
    },
    mounted () {
      this.keyWord = this.bmapSearMsg;
      window.onLoadMap = () => {
        this.ready();
      };
      // key 值需要去高德地图去申请才可以
      asyncJS('https://webapi.amap.com/maps?v=1.4.15&key=自己申请的key&callback=onLoadMap');
    },
    created () {
    },
    beforeDestroy () {
    },
    props: ['bmapSearMsg'],
    methods: {
      closeBmap () {
        this.$emit('closeBmap');
      },
      comfirmBmap () {
        this.$emit('comfirmBmap', this.longAndLat);
      },
      ready () {
        this.map = new window.AMap.Map('container', {
          resizeEnable: true
          });
          let _this = this;
          // 为地图注册click事件获取鼠标点击出的经纬度坐标
          this.map.on('click', function (e) {
            let lng = e.lnglat.getLng();
            let lat = e.lnglat.getLat();
              _this.longAndLat = lng + ',' + lat;
              // 打新的标记
              _this.addMarker(lng, lat);
          });
      },
      search () {
        let _this = this;
        window.AMap.plugin('AMap.PlaceSearch', function () {
          var autoOptions = {
            city: '全国',
            map: _this.map, // 展现结果的地图实例
            pageSize: 1, // 单页显示结果条数
            pageIndex: 1, // 页码
            autoFitView: true // 是否自动调整地图视野使绘制的 Marker点都处于视口的可见范围
          };
          var placeSearch = new window.AMap.PlaceSearch(autoOptions);
          placeSearch.search(_this.keyWord, function (status, result) {
            // 搜索成功时,result即是对应的匹配数据
            if (status == 'complete') {
              if (result.poiList.pois.length > 0) {
                let lng = result.poiList.pois[0].location.lng;
                let lat = result.poiList.pois[0].location.lat;
                _this.longAndLat = lng + ',' + lat;
                _this.addMarker(lng, lat);
              } else {
                _this.$message({
                  message: '没有查询到对应的地址',
                  type: 'warning'
                });
              }
            } else if (status == 'no_data') {
              _this.$message({
                message: '没有查询到对应的地址',
                type: 'warning'
              });
            }
          });
        });
      },
      // 清除 marker
      clearMarker () {
        if (this.mapMark) {
          this.mapMark.setMap(null);
          this.mapMark = null;
        }
      },
      // 实例化点标记
      addMarker (lng, lat) {
        // 先清除原来的标记
        // this.clearMarker();
        // 清除所有标记
        this.map.clearMap();
        let _this = this;
        this.mapMark = new window.AMap.Marker({
            position: [lng, lat],
            map: _this.map
        });
        this.mapMark.setMap(this.map);
      },
      editText () {
        this.isshowTip = false;
      }
    }
  };
</script>
<style lang="less">
  .map-search_wrapper{
    .el-input__inner{
      border:1px solid #96a0a9;
    }
  }
</style>
<style lang="less" scoped>
  .map-search_wrapper {
    position: absolute;
    top: -140px;
    left: 0;
    z-index: 9999;
    border-radius:5px;
    width: 100%;
    min-height: 95%;
    min-width: 950px;
    overflow-x: auto;
    padding: 65px 30px 20px 30px;
    background:#050742;
    color: #000000;
    .header {
      width: 100%;
      height: 60px;
      padding-left: 30px;
      position: absolute;
      top: 0;
      left: 0;
      line-height: 4.5;
      font-size:14px;
      color:#fff;
      .search-input {
        display: inline-block;
        width: 28%;
      }
    }
    #container {
      width: 100%;
      height: 88vh;
      background-color: #FFFFFF;
    }
  }
</style>
// 异步js
export const asyncJS = (src) => {
  var jsapi = document.createElement('script');
  jsapi.charset = 'utf-8';
  jsapi.src = src;
  document.head.appendChild(jsapi);
};

评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值