国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 課堂 > 小程序 > 正文

微信小程序--獲取用戶地理位置名稱(無須用戶授權)的方法

2020-03-21 15:58:35
字體:
來源:轉載
供稿:網友

在本文 微信小程序--獲取用戶地理位置名稱(無須用戶授權) 之前需要先看看

微信小程序-獲取用戶session_key,openid,unionid - 后端為nodejs

代碼封裝是在上文添加的。

準備

1、在http://lbs.qq.com/網站申請key

2、在微信小程序后臺把apis.map.qq.com添加進request合法域名

效果

微信小程序,用戶地理位置,用戶授權

添加封裝

/** * 發起網絡請求 * @param {string} url  * @param {object} params  * @return {Promise}  */ static request(url, params, method = "GET", type = "json") { console.log("向后端傳遞的參數", params); return new Promise((resolve, reject) => {  let opts = {  url: url,  data: Object.assign({}, params),  method: method,  header: { 'Content-Type': type },  success: resolve,  fail: reject  }  // console.log("請求的URL", opts.url);  wx.request(opts); }); }; /** * 獲取用戶中心位置經緯度 * @param {ctx} name  */ static getCenterLocation(name) { return new Promise((resolve, reject) => name.getCenterLocation({ success: resolve, fail: reject })); }

如果已經存在,則不用添加

js

let app = getApp();let wechat = require("../../utils/wechat");Page({ onReady(e) { let mapCtx = wx.createMapContext('myMap'); setTimeout(() => {  mapCtx.moveToLocation(); }, 1000); setTimeout(() => {  this.getAddress(mapCtx); }, 2000); }, getAddress(mapCtx) { wechat.getCenterLocation(mapCtx)  .then(d => {  console.log(d);  let { latitude, longitude } = d;  console.log("當前位置緯度", latitude, "當前位置經度", longitude);  let url = `https://apis.map.qq.com/ws/geocoder/v1/`;  let key = 'XXXXX-D6FAD-RSG4U-HBE6F-NVFNK-XXXXX';  let params = {   location: latitude + "," + longitude,   key  }  return wechat.request(url, params);  })  .then(d => {  console.log(d);  console.log("當前地址", d.data.result.address);  })  .catch(e => {  console.log(e);  }) } })

html

<map id="myMap" show-location="true" scale="16" />

css

page{ height: 100%;}#myMap{ width: 100%; height: 100%;}

參考地址:

https://mp.weixin.qq.com/debug/wxadoc/dev/component/map.html#map
https://mp.weixin.qq.com/debug/wxadoc/dev/api/api-map.html

以上所述是小編給大家介紹的微信小程序-獲取用戶地理位置名稱(無須用戶授權)的方法詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對VEVB武林網網站的支持!


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 莎车县| 南郑县| 塘沽区| 临邑县| 马山县| 分宜县| 绥棱县| 社旗县| 正镶白旗| 涿鹿县| 兴城市| 论坛| 潼南县| 新龙县| 麦盖提县| 通化市| 偃师市| 伊春市| 天台县| 屏东市| 双流县| 浮梁县| 浦县| 台湾省| 新蔡县| 湘阴县| 波密县| 台北县| 理塘县| 界首市| 资溪县| 常山县| 尚志市| 北辰区| 洞口县| 楚雄市| 会东县| 冷水江市| 江达县| 体育| 崇信县|