微信小程序使用腾讯地图实现地点搜索并且随着地图的滑动加载滑动到区域的地点,本文地点使用医院关键词作为搜索地点

实现效果如下

1.页面加载时,根据getLocation方法获取用户当前经纬度获取20条医院位置信息

2.页面滑动时,根据滑动到的经纬度再次获取20条医院位置信息

获取到的医院位置信息

实现方法如下

1.在.wxml中添加触发滑动的方法bindregiοnchange="onMapRegionChange"

html 复制代码
<map id="map" class="map" scale="{{scale}}" markers="{{markers}}" latitude="{{lat}}" show-location longitude="{{lng}}" enable-satellite="{{mapChange}}" bindmarkertap="onMarkerTap" bindregionchange="onMapRegionChange">

2.在.js中

javascript 复制代码
Page({
 data: {
   markers: [],  //覆盖物
   txKey: "你的腾讯地图key",  //腾讯地图key
   regionChanged: false     // 地图区域是否发生变化的标志
 },
 onLoad(options) {
    let that = this
    //获取用户当前位置
    wx.getLocation({
      type: 'gcj02',
      success: function (res) {
        console.log('用户已授权位置权限,经纬度:' + res.longitude, res.latitude);
        that.setData({
          lat: res.latitude,
          lng: res.longitude
        })
        //调用地点搜索方法,把用户当前位置经纬度传递给该方法
        that.getHospitalLoacal(res.longitude,res.latitude)
     }
    })
 },
 //触发滑动方法
 onMapRegionChange: function (e) {
   if (e.type === 'end') {
     this.setData({
      regionChanged: false
    });
    //获取到滑动的经纬度,传递给该方法
    this.getHospitalLoacal(e.detail.centerLocation.longitude, e.detail.centerLocation.latitude);
   }
 },
 //地点搜索方法
 getHospitalLoacal(lng,lat){
 console.log("搜索医院···")
  // 使用腾讯地图API进行关键词搜索
  wx.request({
   url: 'https://apis.map.qq.com/ws/place/v1/search',
   data: {
     keyword: '医院',  // 搜索关键词为"医院"
     /**
      格式:
      boundary=nearby(lat,lng,radius[, auto_extend])
      子参数:
      lat,lng:搜索中心点的经纬度,格式顺序为纬度在前,经度在后
      radius:搜索半径,单位:米,取值范围:10到1000
      auto_extend:[可选] 当前范围无结果时,是否自动扩大范围,取值:
      0 不扩大
      1 [默认] 自动扩大范围(依次按照按1公里、2公里、5公里,
      最大到全城市范围搜索)
     */
     boundary: 'nearby('+lat+','+lng+',1000,1)',
     key: this.data.txKey,
     page_size: 20,//每页条目数,最大限制为20条,默认为10条
     page_index: 1 //第x页,默认第1页
  },
  success: res => {
    if (res.data.status === 0) {
      let hospitals = res.data.data.map(item => {
        return {
           id: item.id,
           longitude: item.location.lng,
           latitude: item.location.lat,
           title: item.title,
           iconPath: '/images/hospital.png', // 自定义标记的图标
           width: 30,
           height: 30
         };
       });

       this.setData({
         markers: hospitals
       });
     } else {
       console.error('地图API请求失败:', res.data.message);
     }
   },
   fail: error => {
     console.error('地图API请求失败:', error);
   }
  });
 }
})



如果本文对你有帮助,记得一键三连哦,你的支持和鼓励就是我最大的动力!^_^

相关推荐
2501_915918413 小时前
使用 HBuilder 上架 iOS 应用时常见的问题与应对方式
android·ios·小程序·https·uni-app·iphone·webview
2501_916007475 小时前
iOS 崩溃日志的分析方法,将崩溃日志与运行过程结合分析
android·ios·小程序·https·uni-app·iphone·webview
2501_916007475 小时前
React Native 混淆在真项目中的方式,当 JS 和原生同时暴露
javascript·react native·react.js·ios·小程序·uni-app·iphone
00后程序员张6 小时前
苹果应用商店上架App流程,签名证书、IPA 校验、上传
android·ios·小程序·https·uni-app·iphone·webview
2501_916007476 小时前
iOS 上架需要哪些准备,围绕证书、描述文件和上传方式等关键环节展开分析
android·ios·小程序·https·uni-app·iphone·webview
qq_12498707536 小时前
基于微信小程序的私房菜定制上门服务系统(源码+论文+部署+安装)
java·spring boot·微信小程序·小程序·毕业设计·毕设
2501_915106326 小时前
iOS 上架费用解析,哪些成本可以通过流程优化降低。
android·ios·小程序·https·uni-app·iphone·webview
换日线°7 小时前
微信小程序找不同游戏(有效果图)
游戏·微信小程序
风月歌7 小时前
小程序项目之超市售货管理平台小程序源代码(源码+文档)
java·微信小程序·小程序·毕业设计·源码
Lily.C8 小时前
小程序WebSocket实时通信全解析
websocket·网络协议·小程序