腾讯地图——当前位置&检索位置

实现检索关键字获取建筑位置列表,类似微信的发送位置功能。

一、效果

二、代码

export default {
  name: 'TMap',
  data() {
    return {
      fromName:'',
      isForward: false,
      searchService:'',
      resultsMap:[],
      searchValue:'',
      currentAddr:{
        name:'',
        address:'',
        latLng:{}
      }
    }
  },
  beforeRouteEnter (to, from, next) {
    console.log(from)
    next(vm => {
      // 通过 `vm` 访问组件实例
      vm.fromName=from.name
    })
  },
  beforeRouteLeave (to, from, next) {
    if (to.name == this.fromName) {
      // 根据是否删除了订单的状态,进行判断list是否需要使用缓存数据
      to.meta.isBack = !this.isForward
    }
    next()
  },
  methods: {
    keyup(){
      let that=this
      that.clearOverlays([])
      that.searchService.search(that.searchValue)
    },
    rlInit:function(){//调用微信坐标
      let that=this
      that.$ajax.post('wechatPay/toConfig',{url1 :location.href.split('#')[0]})
      .then(response=> {
          console.log(response);
          if (response.success) {
            //alert("获取签名成功");
            wx.config({
              beta:true,
              debug : false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
              appId : response.data.weixinAppId, // 必填,公众号的唯一标识
              timestamp : response.data.timestamp, // 必填,生成签名的时间戳
              nonceStr : response.data.noncestr, // 必填,生成签名的随机串
              signature : response.data.signature,// 必填,签名,见附录1
              jsApiList : [ 'getLocation' ] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
            });
            wx.ready(function(){
              wx.getLocation({
                type: 'gcj02', // 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02'
                success: function (res) {
                  console.log(res)
                  let latitude = res.latitude // 纬度,浮点数,范围为90 ~ -90
                  let longitude = res.longitude  // 经度,浮点数,范围为180 ~ -180。
                  let speed = res.speed // 速度,以米/每秒计
                  let accuracy = res.accuracy // 位置精度
                  that.getCuuAddr(latitude,longitude)
                }
              });
            });
          }
      })
    },
    getCuuAddr(lat,lng){
      let that=this
      let load = that.$createToast({txt:'', type:'loading',time: 100000,mask:true}).show()//加载信息
      that.$ajax.post('map/inverseAnalysis',{url:'https://apis.map.qq.com/ws/geocoder/v1/?location='+lat+','+lng+'&get_poi=1&key='+this.$store.state.txKey})
      .then(response=> {
          console.log(response);
          if(response.success){
            that.currentAddr.address=response.data.result.address
            that.currentAddr.name=response.data.result.formatted_addresses.recommend
            that.currentAddr.latLng=response.data.result.location
          }
          load.hide();//关闭加载
      }).catch(error=> {
        load.hide();//关闭加载
      });
    },
    addressSearch(){
      let that=this
      let map = new qq.maps.Map(document.getElementById("map"), {
        // 地图的中心地理坐标。
        center: new qq.maps.LatLng(25.416720, 119.012310),
        zoom: 13
      });
      let latlngBounds = new qq.maps.LatLngBounds();
      //设置Poi检索服务,用于本地检索、周边检索
      that.searchService = new qq.maps.SearchService({
        //设置搜索范围为莆田
        location: "莆田",
        //设置搜索页码为1
        pageIndex: 0,
        //设置每页的结果数为5
        pageCapacity: 30,
        //设置展现查询结构到infoDIV上
        // panel: document.getElementById('infoDiv'),
        //设置动扩大检索区域。默认值true,会自动检索指定城市以外区域。
        autoExtend: true,
        //检索成功的回调函数
        complete: function(results) {
          console.log(results.detail.pois)
          that.resultsMap=results.detail.pois
          map.fitBounds(latlngBounds);
          //调整地图视野
        },
        //若服务请求失败,则运行以下函数
        error: function(error) {
            console.log(error)
            that.resultsMap=[]
        }
      });
    },
    //清除地图上的marker
    clearOverlays(overlays) {
      let overlay;
      while (overlay = overlays.pop()) {
          overlay.setMap(null);
      }
    },
    againLocation(){
      this.rlInit()
    },
    choiceAddr(addr){
      console.log(addr)
      if(addr.address){
        this.isForward=true
        let queryData=this.$common.urlToObj(window.location.href)
        queryData.address=addr.address+addr.name
        queryData.lat=addr.latLng.lat
        queryData.lng=addr.latLng.lng
        queryData.isTMap=true
        this.$router.push({path:'/'+this.fromName,query:queryData})
      }else{
        this.$store.commit('toast',{message:'该地址暂无详细地址,请重新选择',type:'warn',time:2000})
      }
    }
  },
  created() {
    this.rlInit()
  },
  mounted(){
    this.addressSearch()
  }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在Vue中实现腾讯地图的搜索功能,首先需要注册并申请腾讯地图API的key。然后,在Vue项目的/public文件夹中的index.html文件的head标签中引入腾讯地图API的脚本。这可以通过在index.html中添加以下代码来实现: ``` <script src="//map.qq.com/api/js?v=2.exp&key=你的API密钥"></script> <script src="https://map.qq.com/api/gljs?v=1.exp&libraries=service&key=你的API密钥"></script> ``` 接下来,你需要安装lodash和vue-jsonp两个库。可以使用以下命令来安装它们: ``` npm install --save lodash npm install vue-jsonp --save ``` 在Vue的main.js文件中,需要引入vue-jsonp库,并使用Vue.use()来注册vue-jsonp插件: ``` import Vue from 'vue' import { VueJsonp } from 'vue-jsonp' Vue.use(VueJsonp) ``` 最后,你可以在Vue的组件中使用腾讯地图API来进行搜索操作。可以通过使用腾讯地图提供的服务来获取关键词的经纬度和地址,并将地图显示在页面上。具体的代码实现可以根据你的需求和UI设计来进行编写。 请注意,以上代码是基于腾讯地图API的使用示例,你需要根据你自己的腾讯地图API密钥来替换代码中的"你的API密钥"部分。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [vue中使用腾讯API地图,实现搜索关键词获取经纬度和地址并显示地图](https://blog.csdn.net/myjava_6/article/details/126219504)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* [2023年 vue使用腾讯地图搜索、关键字输入提示、地点显示](https://blog.csdn.net/HelloWord176/article/details/131556906)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值