一,编写一个可以任意处都可调用的js文件location.js
// 获取用户经纬度权限
//获取经纬经纬度权限比较简单.不做过多解释.
function getUserLocation() {
let that = this;
wx.getSetting({
success(res) {
if (!res.authSetting["scope.userLocation"]) {
//获取经纬度
wx.authorize({
scope: "scope.userLocation",
success() {
// 授权成功
geo();
},
fail() {
wx.showModal({
title: "提示",
content: "尚未进行授权,获取经纬度",
showCancel: false,
success(res) {
if (res.confirm) {
wx.openSetting({
//这里的方法是调到一个添加权限的页面,可以自己尝试
success: (res) => {
if (!res.authSetting["scope.userLocation"]) {
wx.authorize({
scope: "scope.userLocation",
success() {
// 授权成功
geo();
},
fail() {
console.log("用户点击取消了");
},
});
}
},
fail: function () {
console.log("授权设置失败")