Home  >  Article  >  WeChat Applet  >  Detailed introduction to the steps to implement WeChat mini program using Tencent Map SDK

Detailed introduction to the steps to implement WeChat mini program using Tencent Map SDK

高洛峰
高洛峰Original
2017-03-18 09:27:175698browse

This article implements the use of Tencent Maps in WeChat Mini Programs Maps SDK steps are introduced in detail. Friends in need can refer to the following

Detailed explanation of using Tencent Maps SDK in WeChat Mini Programs And implementation steps

Recently, I used the mini program solution provided by Tencent Maps in a lottery service project, and I would like to share it with you!

It is very simple to use, but some functions still need to be improved.

Official document: lbs.qq.com/qqmap_wx_jssdk/index.html

Steps:

  1. Application Developer key (key): Apply for key

  2. Download WeChat Mini Program JavascriptSDK, WeChat Mini Program JavascriptSDK v1.0

  3. Secure domain name setting, you need to add the domain name address apis.map.qq.com on the WeChat public platform

  4. Mini program example

// 引入SDK核心类
var QQMapWX = require('../../libs/qqmap-wx-jssdk.js');
var qqmapsdk;
Page({
 onLoad: function () {
   // 实例化API核心类
   qqmapsdk = new QQMapWX({
     key: '申请的key'
   });
 },
 onShow: function () {
   // 调用接口
   qqmapsdk.search({
     keyword: '彩票',
     success: function (res) {
       console.log(res);
     },
     fail: function (res) {
       console.log(res);
     },
   complete: function (res) {
     console.log(res);
   }
 });
})

Result renderings:

Detailed introduction to the steps to implement WeChat mini program using Tencent Map SDK

Go to buy lottery.png

5. Core categories

5.1 Location search search(options:Object)

Search surrounding POIs through keywords, such as "hotels", "catering" and "entertainment" ""School" etc.

Go to the implementation of the lottery purchase interface:

5.1.1 buy.js

// 引入腾讯地图SDK核心类
var QQMapWX = require('../../utils/qqmap-wx-jssdk.js');
var util = require("../../utils/util.js");
var qqmapsdk;
Page({
 data: {
  resData: []
 },
 onLoad: function (options) {
  // 实例化腾讯地图API核心类
  qqmapsdk = new QQMapWX({
   key: 'HPNBZ-B426V-CZQPP-UN4R6-QYOF2-MYFU3'//此处使用你自己申请的key
  });
 },
 onShow: function () {
  var that = this;
  // 腾讯地图调用接口
  qqmapsdk.search({
   keyword: '彩票',
   page_size: 20,
   success: function (res) {
    console.log(res);
    var resData = res.data;
    for (var i = 0; i < resData.length; i++) {
     resData[i]._distance = util.formatDistance(resData[i]._distance);//转换一下距离的格式
    }
    that.setData({resData: resData});
   },
   fail: function(res) {
    console.log(res);
   },
   complete: function(res) {
    console.log(res);
   }
  })
 }
})
// utils/util.js
/**
* 将距离格式化
* <1000m时 取整,没有小数点
* >1000m时 单位取km,一位小数点 
*/
function formatDistance(num) {
 if (num < 1000) {
  return num.toFixed(0) + &#39;m&#39;;
 } else if (num > 1000) {
  return (num / 1000).toFixed(1) + &#39;km&#39;;
 }
}

5.1.2 buy.wxml Main The style is weui

<view class="page">
<view wx:for="{{resData}}" wx:key="shop" class="pagebd">
 <view bindtap="navTo" data-item="{{item}}">
  <view class="weui-panel">
   <view class="weui-panelbd">
    <view class="weui-media-box weui-media-box_text">
     <view class="weui-media-boxtitle weui-media-boxtitle_in-text">{{item.title}}</view>
     <view class="weui-media-boxdesc">{{item.address}}</view>
     <view class="weui-media-boxinfo">
      <view class="weui-media-boxinfometa">电话:{{item.tel}}</view>
      <view class="weui-media-boxinfometa weui-media-boxinfometa_extra">距离:{{item._distance}}</view>
     </view>
    </view>
   </view>
  </view>
 </view>
</view>
</view>

5.2 Keyword input prompts getSuggestion(options:Object)

is used to obtain completions and prompts for input keywords to help users input quickly.

Example:

// 调用接口
qqmapsdk.getSuggestion({
 keyword: &#39;技术&#39;,
 success: function(res) {
   console.log(res); 
 },
 fail: function(res) {
   console.log(res);
 },
 complete: function(res) {
   console.log(res);
 }
});

5.3 Distance calculation calculateDistance(options:Object)

Calculate the walking or driving distance from one point to multiple points.

Example:

// 调用接口
qqmapsdk.calculateDistance({
 mode: &#39;walking&#39;;//步行,驾车为&#39;driving&#39;
 to:[{
   latitude: 39.984060,
   longitude: 116.307520
 }, {
   latitude: 39.984572,
   longitude: 116.306339
 }],
 success: function(res) {
   console.log(res);
 },
 fail: function(res) {
   console.log(res);
 },
 complete: function(res) {
   console.log(res);
 }
});

5.4 There are also the following functions, so I won’t demonstrate them one by one.

  1. getCityList(options:Object): Get the national city list data;

  2. getDistrictByCityId(options:Object): Return the city list by city ID Districts and counties;

  3. reverseGeocoder(options:Object): used to obtain completion and prompts for input keywords to help users quickly enter

  4. geocoder(options:Object): Provides conversion from address description to the location coordinates, which is exactly the opposite of the process of reverse geocoder().

The above is the detailed content of Detailed introduction to the steps to implement WeChat mini program using Tencent Map SDK. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn