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

这是一个使用Vue.js编写的组件,实现了地图搜索和查询功能。用户可以输入地址进行查询,点击地图上的点获取经纬度,并通过输入框展示。组件还提供了清除标记和确认填入经纬度的按钮,支持地图的点击事件和高德地图API集成。
摘要由CSDN通过智能技术生成
<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);
};

  • 5
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 13
    评论
实现vue中点击高德地图获取经纬度和详细地址,可以按照以下步骤进行: 1. 在vue项目中安装 `vue-amap` 插件。可以使用npm安装,命令如下: ``` npm install vue-amap --save ``` 2. 在 `main.js` 中引入 `vue-amap` 并进行初始化配置。 ```javascript import VueAMap from 'vue-amap'; Vue.use(VueAMap); VueAMap.initAMapApiLoader({ key: 'your amap key', plugin: ['AMap.Geocoder'] }); ``` 其中 `your amap key` 是你的高德地图开发者Key,需要在高德开放平台上申请。 3. 在组件中引入 `Amap` 组件,并在 `mounted` 钩子函数中进行地图初始化以及监听地图点击事件。 ```vue <template> <div> <div id="map" style="height: 500px;"></div> </div> </template> <script> export default { data() { return { map: null, geocoder: null, marker: null, address: '', lnglat: null } }, mounted() { this.initMap(); this.initGeocoder(); this.addMapClickEvent(); }, methods: { initMap() { this.map = new AMap.Map('map', { zoom: 13, center: [116.397428, 39.90923] }); }, initGeocoder() { this.geocoder = new AMap.Geocoder({ city: "北京" }); }, addMapClickEvent() { let self = this; this.map.on('click', function(e) { self.lnglat = e.lnglat; self.getAddressByLngLat(); self.addMarker(); }); }, getAddressByLngLat() { let self = this; this.geocoder.getAddress(this.lnglat, function(status, result) { if (status === 'complete' && result.regeocode) { self.address = result.regeocode.formattedAddress; } }); }, addMarker() { if (this.marker) { this.map.remove(this.marker); } this.marker = new AMap.Marker({ position: this.lnglat, }); this.map.add(this.marker); } } } </script> ``` 4. 在 `data` 中定义需要用到的变量,如 `map` 表示地图对象、 `geocoder` 表示逆地址解析对象、 `marker` 表示标记点对象、 `address` 表示详细地址、 `lnglat` 表示经纬度。 5. 在 `mounted` 钩子函数中,先调用 `initMap` 方法初始化地图对象,然后调用 `initGeocoder` 方法初始化逆地址解析对象,最后调用 `addMapClickEvent` 方法监听地图点击事件。 6. 在 `addMapClickEvent` 方法中,使用 `map.on('click', function(e) {...})` 监听地图点击事件,并在回调函数中获取点击位置的经纬度并保存到 `lnglat` 变量中,然后调用 `getAddressByLngLat` 方法通过经纬度获取详细地址,并调用 `addMarker` 方法在地图上添加标记点。 7. 在 `getAddressByLngLat` 方法中,调用逆地址解析对象的 `getAddress` 方法,根据经纬度获取详细地址,并将地址保存到 `address` 变量中。 8. 在 `addMarker` 方法中,先判断是否已有标记点,如果有则先移除旧的标记点,然后创建新的标记点并添加到地图上。 9. 最后,在模板中使用 `id="map"` 定义地图容器,并添加样式 `style="height: 500px;"`。
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值