VUE2.0 插入腾讯地图(有经纬度和搜索功能以及标记’)

这段代码展示了如何在网页中集成腾讯地图(TMap)API,实现经纬度输入和地图搜索功能。用户可以输入位置信息进行搜索,点击地图设置标记,并获取标记位置的详细地址。
摘要由CSDN通过智能技术生成

显示效果

代码如下 复制粘贴即可 

//html   显示经纬度 和搜索  地图
         <el-row>
            <el-col :span="12">
              <el-form-item label="经度" prop="longitude">
                <el-input style="width: 200px" disabled v-model="form.longitude"></el-input> </el-form-item
            ></el-col>
            <el-col :span="12">
              <el-form-item label="纬度" prop="latitude">
                <el-input style="width: 200px" disabled v-model="form.latitude"></el-input> </el-form-item
            ></el-col>
          </el-row>
          <el-form-item>
            <div class="map-box">
              <div class="map" ref="map" style="height: 300px" />
              <div class="map-search">
                <el-input v-model="searchValue" placeholder="请输入要检索的位置信息" />
                <el-button @click="searchAddress(searchValue)" type="primary">搜索</el-button>
              </div>
            </div>
          </el-form-item>
<script>
export default {
    data(){
        return{
             searchValue: '', //地图搜索
            form:{
                longitude:'',
                latitude:'',
            }
        }
    },
    created(){
          if (this.script) return;
          this.script = document.createElement('script');
           this.script.type = 'text/javascript';
          this.script.src = `//map.qq.com/api/gljs?v=1.exp&libraries=service&key=GZ5BZ-OAXED-DNM4H-HMVLD-5BIXO-XFBMX`;
         document.head.appendChild(this.script);
         this.script.onload = () => {
         window.initMap = this.initMap;
         this.$nextTick(() => {
         this.initMap();
         });
       };
    },
    method:{
     // 地图
    initMap() {
      // 搜索类
      this.searchEr = new window.TMap.service.Search({ pageSize: 10 });
      // 地图主类
      this.map = new window.TMap.Map(this.$refs.map, {
        backgroundColor: '#f7f7f7',
        mapStyleId: 'style1',
        zoom: 12,
        mapTypeControlOptions: {
          mapTypeIds: [],
        },
        draggableCursor: 'crosshair',
        center: new window.TMap.LatLng(28.616202, 115.942693),
      });
      // 图层类
      this.markerLayer = new window.TMap.MultiMarker({
        map: this.map,
        geometries: [],
      });
      // 地址逆解析
      this.geocoder = new window.TMap.service.Geocoder();
      const setMarker = () => {
        const latlng = new window.TMap.LatLng(this.form.latitude, this.form.longitude);
        this.markerLayer.setGeometries([]);
        let geometries = this.markerLayer.getGeometries();
        geometries.push({
          id: '-1',
          position: latlng,
        });
        this.markerLayer.updateGeometries(geometries);
      };
      this.map.on('click', (e) => {
        this.form.longitude = e.latLng.getLng(); // 经度
        this.form.latitude = e.latLng.getLat(); // 纬度
        setMarker();
        this.getAddressFormat();
      });
      if (this.form.longitude) {
        this.map.setCenter(new window.TMap.LatLng(this.form.latitude, this.form.longitude));
        setMarker();
      }
    },
          // 地图搜索
    searchAddress(keyword = '') {
      if (!keyword) return;
      this.markerLayer.setGeometries([]);
      this.searchEr
        .searchRectangle({
          keyword,
          bounds: this.map.getBounds(),
        })
        .then((result) => {
          const { location = {} } = result.data[0] || {};
          const { lat = 28.616202, lng = 115.942693 } = location;
          this.map.setCenter(new window.TMap.LatLng(lat, lng));
          result.data.forEach((item, index) => {
            let geometries = this.markerLayer.getGeometries();
            // 点标注数据数组
            geometries.push({
              id: String(index),
              position: item.location,
            });
            // 绘制地点标注
            this.markerLayer.updateGeometries(geometries);
          });
        });
    },
    getAddressFormat() {
      const { longitude, latitude } = this.form;
      this.geocoder
        .getAddress({
          location: new window.TMap.LatLng(latitude, longitude),
        })
        .then((res) => {
          const {
            formatted_addresses: { recommend = '' },
          } = res.result || {};
          console.log(recommend, 'recommend');
          this.form.hotelDetailAddress = recommend;
          console.log(this.form);
        });
    },
    }
}
</script>
//样式
<style>
    .map-box {
  position: relative;

  .map-search {
    z-index: 1000;
    display: flex;
    position: absolute;
    top: 20px;
    left: 20px;
    width: 350px;

    button {
      border-radius: 0;
    }
  }
}
.icons {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.time {
  margin-top: 15px;
  width: 100%;
  font-size: 12px;
  flex-wrap: wrap;
  height: 70%;
}
</style>

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值