微信小程序map组件查找周围各种场所(通过腾讯位置服务)

废话不多说,直接写咋实现

主要用到了腾讯位置服务里面的微信小程序JavaScript SDK,你得先申请一个开发者密钥,这里就不多说了

实现效果如下,这里面是搜索周围的餐厅
在这里插入图片描述

首先wxml代码

<map id="map" bindcontroltap="bindControlTap"
bindregionchange="bindRegionChange" longitude="{{longitude}}"
latitude="{{latitude}}" markers="{{markers}}" controls="{{controls}}" style="width:{{mapw}};height:{{maph}}" scale="{{scale}}" show-location>
</map>

bindControlTap事件:主要是点击左下角那个拇指,回到最初的地点
bindRegionChange事件:主要是当你滑动的时候,餐厅也随之发生变化
longitude:经度
latitude:纬度
markers:就是图上的餐厅图片
controls:就是我那个左下角的手指,可以添加多个,位置更改即可
mapw:屏幕宽度
maph:屏幕高度
scale:放大倍数
show-location:展示当前定位点

js代码就得多一点了,首先你要在腾讯位置服务里面下载微信小程序JavaScriptSDK,然后引入qqmap-wx-jssdk.js文件

var QQMapWX = require('../../libs/qqmap-wx-jssdk.js');
var qqmapsdk;
Page({
   data:{
     latitude:23.099994,
     longitude:113.324520,
     mapw:'100%',
     maph:'0',
     scale:'18',
     markers:[],
   },
  mapCtx: null,
  onLoad: function () {
    // 实例化API核心类
    qqmapsdk = new QQMapWX({
      key: ''  //你腾讯位置服务的key
    });
    this.mapCtx = wx.createMapContext('map')
    wx.getSystemInfo({
      success:res=>{
        var mapw = res.windowWidth
        var maph = res.windowHeight
        this.setData({
          maph: maph + 'px',
          controls:[{
            id:1,
            iconPath:'/images/shouzhi.png',
            position:{
              left:10,
              top:maph-50,
              width:30,
              height:20
            },
            clickable:true //可以点击
          }]
        })
      }
  })
  },
  onReady:function(){
    wx.getLocation({
      type:'gcj02',
      success:res=>{
        console.log(res)
        this.setData({
          longitude:res.longitude,
          latitude:res.latitude
        })
        this.getFood(res.longitude,res.latitude)
      }
    })
  },
  //点击回到初始位置
  bindControlTap(e){
    console.log(e.controlId)
    if(e.controlId ===1){
      this.mapCtx.moveToLocation()
    }
  },
  //滑动获取周围的餐厅
  bindRegionChange(e){
   if(e.type ==='end'){
     this.mapCtx.getCenterLocation({
       success:res=>{
         this.getFood(res.longitude,res.latitude)
       }
     })
   }
  },
  getFood: function (longitude, latitude){
    qqmapsdk.search({
      keyword:'餐厅',
      location:{
        longitude: longitude,
        latitude: latitude
      },
      success:res=>{
        var mark = []
        for(let i in res.data){
          mark.push({
            iconPath:'/images/rice.png',
            title: res.data[i].title,
            id:i,
            longitude:res.data[i].location.lng,
            latitude: res.data[i].location.lat,
          })
        }
        mark.push({
          iconPath: '/images/laozhan.png',
          id: res.data.length,
          longitude: longitude,
          latitude: latitude,
        })
        this.setData({
          markers:mark
        })
      }
    })
  },
})
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值