腾讯地图的(地图选点|输入模糊匹配)

简介: 腾讯地图的(地图选点|输入模糊匹配)

1.支持用户输入框输入进行模糊匹配获取详细地址以及经纬度·2.支持用户模糊匹配后点击选点获取详细地址以及经纬度 1.支持用户输入框输入进行模糊匹配获取详细地址以及经纬度·2.支持用户模糊匹配后点击选点获取详细地址以及经纬度

<template>
  <div class="tencentMap-wrap">
    <div class="tencent-container" id="tencent-container"></div>
    <p class="tencent-input">
      <span class="tencent-input-label">地址:</span>
      <input class="tencent-input-style" id="keyword" type="text" v-model="keyword" />
      <ul id="suggestionList">
        <li class="suggestion-li" v-for="(item,index) in suggestionList" :key="index" @click="selectSuggestionHandle(item)">
          <p class="suggestion-li-title">{{item.title}}</p>
          <p class="suggestion-li-content">{{item.address}}</p>
        </li>
      </ul>
    </p>
  </div>
</template>
 
<script>
import loadTMap from '@/utils/loadTMap.js';
const mapInit = loadTMap('PZHBZ-G6A34-QV7UJ-X4QXK-YIMRK-RAFZS');
let map =null;
let marker = null;
export default {
  data() {
    return {
      suggestionList:[],
      keyword:''
    };
  },
  watch:{
    keyword(value){
      let keyword=this.trim(value)
      if(keyword){
        this.searchSuggestionAPI(keyword)
      }
    }
  },
  created() {
    this.initMap();
  },
  mounted() {},
  methods: {
    /**
     * 初始化地图
     * **/
    initMap() {
      mapInit.then(TMap => {
        //定义map变量,调用TMap.Map构造函数创建地图
        map = new TMap.Map(document.getElementById('tencent-container'), {
          center: new TMap.LatLng(31.230355, 121.47371), //设置地图中心点坐标
          zoom: 17 //设置地图缩放级别
        });
        
        const geocoder = new TMap.service.Geocoder(); // 新建一个正逆地址解析类
        //绑定点击事件
        map.on('click', (evt)=> {
          if (marker) {
            marker.setMap(null);
            marker = null;
          }
 
          const lat = evt.latLng.getLat().toFixed(6);
          const lng = evt.latLng.getLng().toFixed(6);
          const location = new TMap.LatLng(lat, lng);
          geocoder
          .getAddress({ 
            location:location,
          }) // 将给定的坐标位置转换为地址
          .then((result) => {
            console.log(evt)
            console.log(result)
            this.$emit("tMap",{
              title:(evt.poi!=null?evt.poi.name:result.result.formatted_addresses.recommend),
              address:result.result.address,
              lat:lat,
              lng:lng
            })
          });
 
          if (!marker) {
            marker = new TMap.MultiMarker({
              map: map,
              geometries: [
                {
                  position: new TMap.LatLng(lat, lng)
                }
              ]
            });
          }
        });
        
      });
    },
    /**
     * 搜索
     * **/
    searchSuggestionAPI(keyword){
      mapInit.then(TMap => {
        // 新建一个关键字输入提示类
        var suggest = new TMap.service.Suggestion({
          pageSize: 10 // 返回结果每页条目数
        });
        suggest.getSuggestions({ keyword: keyword })
        .then((result) => {
            // 以当前所输入关键字获取输入提示
          this.suggestionList=result.data;
        })
        .catch((error) => {
          console.log(error);
        });
        
      })
    },
    /**
     * 点击选中下拉数据
     * **/
    selectSuggestionHandle(item){
      this.createMarkerAPI(item.location.lat,item.location.lng);
      this.$emit("tMap",{
        title:item.title,
        address:item.address,
        lat:item.location.lat,
        lng:item.location.lng
      })
      this.suggestionList=[]
    },
    createMarkerAPI(lat, lng){
      map.setCenter({
        lat, lng
      });
      mapInit.then(TMap => {
        if (marker) {
          marker.setMap(null);
          marker = null;
        }
                
        if (!marker) {
          marker = new TMap.MultiMarker({
            map: map,
            geometries: [
              {
                position: new TMap.LatLng(lat, lng)
              }
            ]
          });
        }
        
      });
    },
    /**
     * 去掉空隙
     * **/
    trim(str, type) {
      str = str || ''
      type = type || 1
      switch (type) {
        case 1:
          return str.replace(/\s+/g, "");
        case 2:
          return str.replace(/(^\s*)|(\s*$)/g, "");
        case 3:
          return str.replace(/(^\s*)/g, "");
        case 4:
          return str.replace(/(\s*$)/g, "");
        default:
          return str;
      }
    }
  }
};
</script>
 
<style lang="scss" scoped>
.tencentMap-wrap {
  width: 100%;
  .tencent-container {
    width: 100%;
    height: 400px;
  }
  .tencent-input {
    width: 100%;
    margin-top: 10px;
    position: relative;
    .tencent-input-label {
      font-size: 16px;
      color: #333;
      padding-right: 10px;
    }
    .tencent-input-style {
      border: 1px solid #dcdcdc;
      width: 200px;
      height: 40px;
      border-radius: 8px;
      padding-left: 12px;
      padding-right: 12px;
    }
    #suggestionList{
      border-radius: 8px;
      width: 400px;
      max-height: 200px;
      overflow-y: auto;
      background-color: #fff;
      position: absolute;
      top:40px;
      left: 58px;
      z-index: 99;
      display: flex;
      flex-direction: column;
      .suggestion-li{
        width: 91.47%;
        margin: auto;
        display: flex;
        flex-direction: column;
        padding: 5px 0px;
        cursor: pointer;
        .suggestion-li-title{
          font-size: 16px;
          color: #333;
        }
        .suggestion-li-content{
          font-size: 15px;
          color: #999;
          margin-top: 5px;
        }
      }
    }
  }
}
</style>
 
 
相关文章
|
8月前
|
人工智能 算法 定位技术
基于等照度线和窗口匹配的图像修补算法
基于等照度线和窗口匹配的图像修补算法
|
Linux
【PyAutoGUI操作指南】05 屏幕截图与图像定位:截图+定位单个目标+定位全部目标+灰度匹配+像素匹配+获取屏幕截图中像素的RGB颜色
【PyAutoGUI操作指南】05 屏幕截图与图像定位:截图+定位单个目标+定位全部目标+灰度匹配+像素匹配+获取屏幕截图中像素的RGB颜色
719 0
|
1月前
|
JavaScript 前端开发 小程序
高德地图实现-逆地理编码-输入提示-地图标点-实现车库管理
高德地图实现-逆地理编码-输入提示-地图标点-实现车库管理
|
1月前
LabVIEW如何获取波形图上游标所在位置的数值
LabVIEW如何获取波形图上游标所在位置的数值
18 0
|
1月前
|
算法
[Halcon&定位] 形状匹配和灰度匹配对比
[Halcon&定位] 形状匹配和灰度匹配对比
118 0
|
1月前
|
前端开发 JavaScript 定位技术
高德地图精确到某个位置
高德地图精确到某个位置
36 0
|
8月前
|
定位技术
ArcMap | 出图小技巧——比例尺、鹰眼图、表格、文本、图片
ArcMap | 出图小技巧——比例尺、鹰眼图、表格、文本、图片
206 0
|
定位技术 C# Windows
C#编程学习(05):使用webbroswer控件显示地图并标注点位坐标
C#编程学习(05):使用webbroswer控件显示地图并标注点位坐标
|
11月前
|
JSON 定位技术 API
百度地图高级开发:获取某范围半径圆形区域检索覆盖物内的所有标注的解决方案(1)
百度地图高级开发:获取某范围半径圆形区域检索覆盖物内的所有标注的解决方案(1)
196 0
|
Windows
windows自带的比微信好用的截图工具:截取任意形状图片,标尺画直线,窗口图精准截取
windows自带的比微信好用的截图工具:截取任意形状图片,标尺画直线,窗口图精准截取
338 0