微信小程序调用腾讯地图-并解读API文档 JavaScript SDK和 WebService API ...

打印 上一主题 下一主题

主题 1031|帖子 1031|积分 3093

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
搜索:腾讯位置服务
   

  找到API文档:
   

  入门中第一步:申请开发者密钥key
   

  前往控制台:
  

  创建应用并获取key:
  

  设置key的时候,还需要小程序的APPID。以是要前往微信公众平台中获取小程序的APPID:
  

  限定要求:
   

  添加配额:
  

  看清哪一个key而且设置配额。假如有多个key,也可以根据特别的某些key举行分配额度:
  

  下载地图的SDK:
   

  并放入项目中:
  

  添加服务器域名白名单:
   

  登录微信公众平台:
  

  设置白名单:
  

  搜索地点:
   

  实际开发者工具中截图:
  

  坐标地图:
  

  wxml:

  最终展示: 点击搜索周边KFC就出现赤色的预设值坐标的地点。
  

  具体代码:
   map.js
 
  1. // 引入SDK核心类
  2. var QQMapWX = require('../../libs/qqmap-wx-jssdk.js');
  3. Page({
  4.   data: {
  5.     markers: []
  6.   },
  7.   onLoad: function () {
  8.     // 实例化API核心类
  9.     this.qqmapsdk = new QQMapWX({
  10.       key: '************************ // 替换为你的QQ地图SDK密钥
  11.     });
  12.   },
  13.   // 事件触发,调用接口
  14.   nearby_search: function () {
  15.     var _this = this;
  16.     // 调用接口
  17.     this.qqmapsdk.search({
  18.       keyword: 'kfc',  // 搜索关键词
  19.       location: '39.980014,116.313972',  // 设置周边搜索中心点
  20.       success: function (res) { // 搜索成功后的回调
  21.         var mks = [];
  22.         for (var i = 0; i < res.data.length; i++) {
  23.           mks.push({ // 获取返回结果,放到mks数组中
  24.             title: res.data[i].title,
  25.             id: parseInt(res.data[i].id), // 将 id 转换为整数形式
  26.             latitude: res.data[i].location.lat,
  27.             longitude: res.data[i].location.lng,
  28.             iconPath: "/assets/icon/position.png", // 图标路径
  29.             width: 20,
  30.             height: 20
  31.           });
  32.         }
  33.         _this.setData({ // 设置markers属性,将搜索结果显示在地图中
  34.           markers: mks
  35.         });
  36.       },
  37.       fail: function (res) {
  38.         console.log('搜索失败', res);
  39.       },
  40.       complete: function (res) {
  41.         console.log('搜索完成', res);
  42.       }
  43.     });
  44.   }
  45. });
复制代码
wxml:
 
  1. <!--绑定点击事件-->
  2. <button bindtap="nearby_search">搜索周边KFC</button>
  3. <!--地图容器-->
  4. <map id="myMap"
  5.    markers="{{markers}}"
  6.    style="width:100%;height:300px;"
  7.    longitude="116.313972"
  8.    latitude="39.980014" scale='16'>
  9. </map>
复制代码

  
注意:
1 调用API有次数和额度限定。
2 调用的接口要开通相应的接口权限。


示例2:  “关键词输入提示”
   

  接口调用阐明:
   

  前往开通配额:
  

  代码:
   wxml:
  

  实际wxml:
  

  **.js 注意js代码不要全部拷贝,而是将methods的部分放在Page()中:
  

  实际**.js:
  

  
  最后展示:
   

  
调用WebService API
   

  举例:定位服务 --IP定位
   

  wxml:
  1. <view class="container">
  2.   <view class="map-container">
  3.     <map id="map" latitude="{{latitude}}" longitude="{{longitude}}" markers="{{markers}}" style="width: 100%; height: 400px;"></map>
  4.   </view>
  5.   <view class="info-container">
  6.     <view class="info-item">
  7.       <text class="info-label">国家:</text>
  8.       <text class="info-value">{{nation}}</text>
  9.     </view>
  10.     <view class="info-item">
  11.       <text class="info-label">省份:</text>
  12.       <text class="info-value">{{province}}</text>
  13.     </view>
  14.     <view class="info-item">
  15.       <text class="info-label">城市:</text>
  16.       <text class="info-value">{{city}}</text>
  17.     </view>
  18.   </view>
  19. </view>
复制代码
js:
  1. Page({
  2.   data: {
  3.     latitude: 0, // 地图中心点纬度
  4.     longitude: 0, // 地图中心点经度
  5.     markers: [], // 地图标记点
  6.     nation: '', // 国家
  7.     province: '', // 省份
  8.     city: '', // 城市
  9.   },
  10.   onLoad: function () {
  11.     // 发起获取当前IP定位信息的请求
  12.     this.getLocationByIP();
  13.   },
  14.   getLocationByIP: function () {
  15.     // 替换成你自己申请的腾讯地图API密钥
  16.     const key = '************************';
  17.     const apiUrl = `https://apis.map.qq.com/ws/location/v1/ip?key=${key}`;
  18.     wx.request({
  19.       url: apiUrl,
  20.       method: 'GET',
  21.       success: (res) => {
  22.         console.log('IP定位结果:', res.data);
  23.         if (res.data.status === 0) {
  24.           const { location, ad_info } = res.data.result;
  25.           const { lat, lng } = location;
  26.           const { nation, province, city } = ad_info;
  27.           // 更新页面数据,显示定位信息
  28.           this.setData({
  29.             latitude: lat,
  30.             longitude: lng,
  31.             markers: [{
  32.               id: 1,
  33.               latitude: lat,
  34.               longitude: lng,
  35.               title: city,
  36.               iconPath: '/images/location.png', // 可自定义标记图标
  37.               width: 30,
  38.               height: 30
  39.             }],
  40.             nation: nation,
  41.             province: province,
  42.             city: city
  43.           });
  44.         } else {
  45.           wx.showToast({
  46.             title: '定位失败,请稍后重试',
  47.             icon: 'none',
  48.             duration: 2000
  49.           });
  50.         }
  51.       },
  52.       fail: (error) => {
  53.         console.error('请求失败:', error);
  54.         wx.showToast({
  55.           title: '网络请求失败,请检查网络后重试',
  56.           icon: 'none',
  57.           duration: 2000
  58.         });
  59.       }
  60.     });
  61.   }
  62. });
复制代码
wxss:
  1. .container {
  2.   display: flex;
  3.   flex-direction: column;
  4.   justify-content: center;
  5.   align-items: center;
  6.   padding: 20px;
  7. }
  8. .map-container {
  9.   width: 100%;
  10.   margin-bottom: 20px;
  11. }
  12. .info-container {
  13.   width: 100%;
  14.   background-color: #f0f0f0;
  15.   padding: 10px;
  16.   border-radius: 8px;
  17. }
  18. .info-item {
  19.   display: flex;
  20.   flex-direction: row;
  21.   margin-bottom: 5px;
  22. }
  23. .info-label {
  24.   font-weight: bold;
  25. }
  26. .info-value {
  27.   margin-left: 5px;
  28. }
复制代码
最终展示:
  

  

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

王海鱼

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表