uniapp中位置授权提示

前言

因项目的需求,需要,首次登录小程序的用户,一进来就显示一个弹窗,提示

点击允许打印的结果

官网地址     uni.getSetting   获取用户的当前设置。

出现弹窗的原因是因为使用 了 uni.authorize 这个属性 

还需要在  manifest.json 中配置

总代码 

实现弹层的方法也就是 下面那两个方法以及 在 manifest.json 文件中配置,后面的  getLocation 以及 后续补充的  chooseSpot 是获取当前城市位置 以及经纬度的方法

获取位置授权代码

在methods中      

 isGetLocation(a = "scope.userLocation") { //检查当前是否已经授权访问scope属性
        var _this = this;
        uni.getSetting({
          success(res) {
            console.log(res)
            if (!res.authSetting[a]) { //每次进入程序判断当前是否获得授权,如果没有就去获得授权,如果获得授权,就直接获取当前地理位置
              _this.getAuthorizeInfo()
            } else {
              _this.getLocation();      
            }
          }
        });
      },
      getAuthorizeInfo(a = "scope.userLocation") { // uniapp弹窗弹出获取授权(地理,个人微信信息等授权信息)弹窗
        var _this = this;
        uni.authorize({
          scope: a,
          success() { //允许授权
            _this.getLocation();   
          }
        })
      },
      getLocation() {
        uni.getLocation({
          type: 'gcj02',
          geocode: true,
          success: res => {
            this.latitude = res.latitude
            this.longitude = res.longitude
            let latlng = [this.latitude, this.longitude]
            console.log('当前位置的经纬度', latlng);
            // console.log(res, '位置信息打印查看结果');
          },
          address: (res) => {
            console.log('address', res);
          }
        });
      },
onShow() {
      this.getLocation()
    },

提示,要注意一个问题,就是当 测试版发布为线上版本时,微信小程序会审核uni.getLocation这个api的接口,

后续

若不想审核 uni.getLocation这个api的接口, 可以使用 下面的方法

引入文件

 import amap from '../../common/amap-wx.130.js'

methods中

 isGetLocation(a = "scope.userLocation") { //检查当前是否已经授权访问scope属性
        var _this = this;
        uni.getSetting({
          success(res) {
            console.log('用户同意获取地理位置设置 getSetting', res)
            if (!res.authSetting[a]) { //每次进入程序判断当前是否获得授权,如果没有就去获得授权,如果获得授权,就直接获取当前地理位置
              _this.getAuthorizeInfo()
            } else {
              _this.chooseSpot();
            }
          },
          fail(err) {
            console.log('用户拒绝授权', err);
          }
        });

      },
      getAuthorizeInfo(a = "scope.userLocation") { // uniapp弹窗弹出获取授权(地理,个人微信信息等授权信息)弹窗
        var _this = this;
        uni.authorize({
          scope: a,
          success() { //允许授权
            console.log('已授权');
            _this.getStationList()
          },
          fail(err) {
            console.log('用户拒绝', err);
            _this.getStationList()
          }
        })

      },
      chooseSpot() { // 高德地图的方法,获取地址
        this.amapPlugin.getRegeo({
          success: (data) => {
            this.latitude = data[0].latitude
            this.longitude = data[0].longitude
            this.city = data[0].regeocodeData.addressComponent.district
            console.log('用户当前城市显示的是:', this.city);
            this.updateUserCity(this.city)
            this.getStationList()
          }
        });
      },
onLoad(){

    this.amapPlugin = new amap.AMapWX({
        key: this.key
      });
      this.isGetLocation()
}
data 里面定义

 // 在高德小程序中申请的key
 key: '7ae9ca9d5e47feb6d3c69XXXXXXXX',
 amapPlugin: null

配置项

 "permission": {
      "scope.userLocation": {
        "desc": "获取您的位置"
      }
    },
    "lazyCodeLoading": "requiredComponents",
    "requiredPrivateInfos": [
      "getLocation",
      "onLocationChange",
      "startLocationUpdateBackground",
      "chooseAddress"
    ]

js文件

function AMapWX(a) {
  this.key = a.key;
  this.requestConfig = {
    key: a.key,
    s: "rsx",
    platform: "WXJS",
    appname: a.key,
    sdkversion: "1.2.0",
    logversion: "2.0"
  };
  this.MeRequestConfig = {
    key: a.key,
    serviceName: "https://restapi.amap.com/rest/me"
  }
}
AMapWX.prototype.getWxLocation = function(a, b) {
  uni.getLocation({
    type: "gcj02",
    success: function(c) {
      c = c.longitude + "," + c.latitude;

      uni.setStorage({
        key: "userLocation",
        data: c
      });
      b(c)
    },
    fail: function(c) {
      uni.getStorage({
        key: "userLocation",
        success: function(d) {
          d.data && b(d
            .data)
        }
      });
      console.log(c);
      a.fail({
        errCode: "0",
        errMsg: c.errMsg || ""
      })
    }
  })
};
AMapWX.prototype.getMEKeywordsSearch = function(a) {
  if (!a.options) return a.fail({
    errCode: "0",
    errMsg: "\u7f3a\u5c11\u5fc5\u8981\u53c2\u6570"
  });
  var b = a.options,
    c = this.MeRequestConfig,
    d = {
      key: c.key,
      s: "rsx",
      platform: "WXJS",
      appname: a.key,
      sdkversion: "1.2.0",
      logversion: "2.0"
    };
  b.layerId && (d.layerId = b.layerId);
  b.keywords && (d.keywords = b.keywords);
  b.city && (d.city = b.city);
  b.filter && (d.filter = b.filter);
  b.sortrule && (d.sortrule = b.sortrule);
  b.pageNum && (d.pageNum = b.pageNum);
  b.pageSize && (d.pageSize = b.pageSize);
  b.sig && (d.sig =
    b.sig);
  uni.request({
    url: c.serviceName + "/cpoint/datasearch/local",
    data: d,
    method: "GET",
    header: {
      "content-type": "application/json"
    },
    success: function(e) {
      (e = e.data) && e.status && "1" === e.status && 0 === e.code ? a.success(e.data) : a.fail({
        errCode: "0",
        errMsg: e
      })
    },
    fail: function(e) {
      a.fail({
        errCode: "0",
        errMsg: e.errMsg || ""
      })
    }
  })
};
AMapWX.prototype.getMEIdSearch = function(a) {
  if (!a.options) return a.fail({
    errCode: "0",
    errMsg: "\u7f3a\u5c11\u5fc5\u8981\u53c2\u6570"
  });
  var b = a.options,
    c = this.MeRequestConfig,
    d = {
      key: c.key,
      s: "rsx",
      platform: "WXJS",
      appname: a.key,
      sdkversion: "1.2.0",
      logversion: "2.0"
    };
  b.layerId && (d.layerId = b.layerId);
  b.id && (d.id = b.id);
  b.sig && (d.sig = b.sig);
  uni.request({
    url: c.serviceName + "/cpoint/datasearch/id",
    data: d,
    method: "GET",
    header: {
      "content-type": "application/json"
    },
    success: function(e) {
      (e = e.data) && e.status && "1" === e.status &&
        0 === e.code ? a.success(e.data) : a.fail({
          errCode: "0",
          errMsg: e
        })
    },
    fail: function(e) {
      a.fail({
        errCode: "0",
        errMsg: e.errMsg || ""
      })
    }
  })
};
AMapWX.prototype.getMEPolygonSearch = function(a) {
  if (!a.options) return a.fail({
    errCode: "0",
    errMsg: "\u7f3a\u5c11\u5fc5\u8981\u53c2\u6570"
  });
  var b = a.options,
    c = this.MeRequestConfig,
    d = {
      key: c.key,
      s: "rsx",
      platform: "WXJS",
      appname: a.key,
      sdkversion: "1.2.0",
      logversion: "2.0"
    };
  b.layerId && (d.layerId = b.layerId);
  b.keywords && (d.keywords = b.keywords);
  b.polygon && (d.polygon = b.polygon);
  b.filter && (d.filter = b.filter);
  b.sortrule && (d.sortrule = b.sortrule);
  b.pageNum && (d.pageNum = b.pageNum);
  b.pageSize && (d.pageSize = b.pageSize);
  b.sig && (d.sig = b.sig);
  uni.request({
    url: c.serviceName + "/cpoint/datasearch/polygon",
    data: d,
    method: "GET",
    header: {
      "content-type": "application/json"
    },
    success: function(e) {
      (e = e.data) && e.status && "1" === e.status && 0 === e.code ? a.success(e.data) : a.fail({
        errCode: "0",
        errMsg: e
      })
    },
    fail: function(e) {
      a.fail({
        errCode: "0",
        errMsg: e.errMsg || ""
      })
    }
  })
};
AMapWX.prototype.getMEaroundSearch = function(a) {
  if (!a.options) return a.fail({
    errCode: "0",
    errMsg: "\u7f3a\u5c11\u5fc5\u8981\u53c2\u6570"
  });
  var b = a.options,
    c = this.MeRequestConfig,
    d = {
      key: c.key,
      s: "rsx",
      platform: "WXJS",
      appname: a.key,
      sdkversion: "1.2.0",
      logversion: "2.0"
    };
  b.layerId && (d.layerId = b.layerId);
  b.keywords && (d.keywords = b.keywords);
  b.center && (d.center = b.center);
  b.radius && (d.radius = b.radius);
  b.filter && (d.filter = b.filter);
  b.sortrule && (d.sortrule = b.sortrule);
  b.pageNum && (d.pageNum = b.pageNum);
  b.pageSize &&
    (d.pageSize = b.pageSize);
  b.sig && (d.sig = b.sig);
  uni.request({
    url: c.serviceName + "/cpoint/datasearch/around",
    data: d,
    method: "GET",
    header: {
      "content-type": "application/json"
    },
    success: function(e) {
      (e = e.data) && e.status && "1" === e.status && 0 === e.code ? a.success(e.data) : a.fail({
        errCode: "0",
        errMsg: e
      })
    },
    fail: function(e) {
      a.fail({
        errCode: "0",
        errMsg: e.errMsg || ""
      })
    }
  })
};
AMapWX.prototype.getGeo = function(a) {
  var b = this.requestConfig,
    c = a.options;
  b = {
    key: this.key,
    extensions: "all",
    s: b.s,
    platform: b.platform,
    appname: this.key,
    sdkversion: b.sdkversion,
    logversion: b.logversion
  };
  c.address && (b.address = c.address);
  c.city && (b.city = c.city);
  c.batch && (b.batch = c.batch);
  c.sig && (b.sig = c.sig);
  uni.request({
    url: "https://restapi.amap.com/v3/geocode/geo",
    data: b,
    method: "GET",
    header: {
      "content-type": "application/json"
    },
    success: function(d) {
      (d = d.data) && d.status && "1" === d.status ? a.success(d) : a.fail({
        errCode: "0",
        errMsg: d
      })
    },
    fail: function(d) {
      a.fail({
        errCode: "0",
        errMsg: d.errMsg || ""
      })
    }
  })
};
AMapWX.prototype.getRegeo = function(a) {
  function b(d) {
    var e = c.requestConfig;
    uni.request({
      url: "https://restapi.amap.com/v3/geocode/regeo",
      data: {
        key: c.key,
        location: d,
        extensions: "all",
        s: e.s,
        platform: e.platform,
        appname: c.key,
        sdkversion: e.sdkversion,
        logversion: e.logversion
      },
      method: "GET",
      header: {
        "content-type": "application/json"
      },
      success: function(g) {
        if (g.data.status && "1" == g.data.status) {
          g = g.data.regeocode;
          var h = g.addressComponent,
            f = [],
            k = g.roads[0].name + "\u9644\u8fd1",
            m = d.split(",")[0],
            n = d.split(",")[1];
          if (g.pois &&
            g.pois[0]) {
            k = g.pois[0].name + "\u9644\u8fd1";
            var l = g.pois[0].location;
            l && (m = parseFloat(l.split(",")[0]), n = parseFloat(l.split(",")[1]))
          }
          h.provice && f.push(h
            .provice);
          h.city && f.push(h.city);
          h.district && f.push(h.district);
          h.streetNumber && h.streetNumber.street && h.streetNumber.number ? (f.push(h.streetNumber.street), f
            .push(h.streetNumber.number)) : f.push(g.roads[0].name);
          f = f.join("");
          a.success([{
            iconPath: a.iconPath,
            width: a.iconWidth,
            height: a.iconHeight,
            name: f,
            desc: k,
            longitude: m,
            latitude: n,
            id: 0,
            regeocodeData: g
          }])
        } else a.fail({
          errCode: g.data.infocode,
          errMsg: g.data.info
        })
      },
      fail: function(g) {
        a.fail({
          errCode: "0",
          errMsg: g.errMsg || ""
        })
      }
    })
  }
  var c = this;
  a.location ? b(a.location) : c.getWxLocation(a, function(d) {
    b(d)
  })
};
AMapWX.prototype.getWeather = function(a) {
  function b(g) {
    var h = "base";
    a.type && "forecast" == a.type && (h = "all");
    uni.request({
      url: "https://restapi.amap.com/v3/weather/weatherInfo",
      data: {
        key: d.key,
        city: g,
        extensions: h,
        s: e.s,
        platform: e.platform,
        appname: d.key,
        sdkversion: e
          .sdkversion,
        logversion: e.logversion
      },
      method: "GET",
      header: {
        "content-type": "application/json"
      },
      success: function(f) {
        if (f.data.status && "1" == f.data.status)
          if (f.data.lives) {
            if ((f = f.data.lives) && 0 < f.length) {
              f = f[0];
              var k = {
                city: {
                  text: "\u57ce\u5e02",
                  data: f.city
                },
                weather: {
                  text: "\u5929\u6c14",
                  data: f.weather
                },
                temperature: {
                  text: "\u6e29\u5ea6",
                  data: f.temperature
                },
                winddirection: {
                  text: "\u98ce\u5411",
                  data: f.winddirection + "\u98ce"
                },
                windpower: {
                  text: "\u98ce\u529b",
                  data: f.windpower + "\u7ea7"
                },
                humidity: {
                  text: "\u6e7f\u5ea6",
                  data: f.humidity + "%"
                }
              };
              k.liveData = f;
              a.success(k)
            }
          } else f.data.forecasts && f.data.forecasts[0] && a.success({
            forecast: f.data.forecasts[0]
          });
        else a.fail({
          errCode: f.data.infocode,
          errMsg: f.data.info
        })
      },
      fail: function(f) {
        a.fail({
          errCode: "0",
          errMsg: f.errMsg || ""
        })
      }
    })
  }

  function c(g) {
    uni.request({
      url: "https://restapi.amap.com/v3/geocode/regeo",
      data: {
        key: d.key,
        location: g,
        extensions: "all",
        s: e.s,
        platform: e.platform,
        appname: d.key,
        sdkversion: e.sdkversion,
        logversion: e.logversion
      },
      method: "GET",
      header: {
        "content-type": "application/json"
      },
      success: function(h) {
        if (h.data.status && "1" == h.data.status) {
          h = h.data.regeocode;
          if (h
            .addressComponent) var f = h.addressComponent.adcode;
          else h.aois && 0 < h.aois.length && (f = h.aois[0].adcode);
          b(f)
        } else a.fail({
          errCode: h.data.infocode,
          errMsg: h.data.info
        })
      },
      fail: function(h) {
        a.fail({
          errCode: "0",
          errMsg: h.errMsg || ""
        })
      }
    })
  }
  var d = this,
    e = d.requestConfig;
  a.city ? b(a.city) : d.getWxLocation(a, function(g) {
    c(g)
  })
};
AMapWX.prototype.getPoiAround = function(a) {
  function b(e) {
    e = {
      key: c.key,
      location: e,
      s: d.s,
      platform: d.platform,
      appname: c.key,
      sdkversion: d.sdkversion,
      logversion: d.logversion
    };
    a.querytypes && (e.types = a.querytypes);
    a.querykeywords && (e.keywords = a.querykeywords);
    uni.request({
      url: "https://restapi.amap.com/v3/place/around",
      data: e,
      method: "GET",
      header: {
        "content-type": "application/json"
      },
      success: function(g) {
        if (g.data.status && "1" == g.data.status) {
          if ((g = g.data) && g.pois) {
            for (var h = [], f = 0; f < g.pois.length; f++) {
              var k = 0 ==
                f ? a.iconPathSelected : a.iconPath;
              h.push({
                latitude: parseFloat(g.pois[f].location.split(",")[1]),
                longitude: parseFloat(g.pois[f]
                  .location.split(",")[0]),
                iconPath: k,
                width: 22,
                height: 32,
                id: f,
                name: g.pois[f].name,
                address: g.pois[f].address
              })
            }
            a.success({
              markers: h,
              poisData: g.pois
            })
          }
        } else a.fail({
          errCode: g.data.infocode,
          errMsg: g.data.info
        })
      },
      fail: function(g) {
        a.fail({
          errCode: "0",
          errMsg: g.errMsg || ""
        })
      }
    })
  }
  var c = this,
    d = c.requestConfig;
  a.location ? b(a.location) : c.getWxLocation(a, function(e) {
    b(e)
  })
};
AMapWX.prototype.getStaticmap = function(a) {
  function b(e) {
    c.push("location=" + e);
    a.zoom && c.push("zoom=" + a.zoom);
    a.size && c.push("size=" + a.size);
    a.scale && c.push("scale=" + a.scale);
    a.markers && c.push("markers=" + a.markers);
    a.labels && c.push("labels=" + a.labels);
    a.paths && c.push("paths=" + a.paths);
    a.traffic && c.push("traffic=" + a.traffic);
    e = "https://restapi.amap.com/v3/staticmap?" + c.join("&");
    a.success({
      url: e
    })
  }
  var c = [];
  c.push("key=" + this.key);
  var d = this.requestConfig;
  c.push("s=" + d.s);
  c.push("platform=" + d.platform);
  c.push("appname=" + d.appname);
  c.push("sdkversion=" + d.sdkversion);
  c.push("logversion=" + d.logversion);
  a.location ? b(a.location) : this.getWxLocation(a, function(e) {
    b(e)
  })
};
AMapWX.prototype.getInputtips = function(a) {
  var b = Object.assign({}, this.requestConfig);
  a.location && (b.location = a.location);
  a.keywords && (b.keywords = a.keywords);
  a.type && (b.type = a.type);
  a.city && (b.city = a.city);
  a.citylimit && (b.citylimit = a.citylimit);
  uni.request({
    url: "https://restapi.amap.com/v3/assistant/inputtips",
    data: b,
    method: "GET",
    header: {
      "content-type": "application/json"
    },
    success: function(c) {
      c && c.data && c.data.tips && a.success({
        tips: c.data.tips
      })
    },
    fail: function(c) {
      a.fail({
        errCode: "0",
        errMsg: c.errMsg ||
          ""
      })
    }
  })
};
AMapWX.prototype.getDrivingRoute = function(a) {
  var b = Object.assign({}, this.requestConfig);
  a.origin && (b.origin = a.origin);
  a.destination && (b.destination = a.destination);
  a.strategy && (b.strategy = a.strategy);
  a.waypoints && (b.waypoints = a.waypoints);
  a.avoidpolygons && (b.avoidpolygons = a.avoidpolygons);
  a.avoidroad && (b.avoidroad = a.avoidroad);
  uni.request({
    url: "https://restapi.amap.com/v3/direction/driving",
    data: b,
    method: "GET",
    header: {
      "content-type": "application/json"
    },
    success: function(c) {
      c && c.data && c.data.route && a.success({
        paths: c.data.route.paths,
        taxi_cost: c.data.route.taxi_cost || ""
      })
    },
    fail: function(c) {
      a.fail({
        errCode: "0",
        errMsg: c.errMsg || ""
      })
    }
  })
};
AMapWX.prototype.getWalkingRoute = function(a) {
  var b = Object.assign({}, this.requestConfig);
  a.origin && (b.origin = a.origin);
  a.destination && (b.destination = a.destination);
  uni.request({
    url: "https://restapi.amap.com/v3/direction/walking",
    data: b,
    method: "GET",
    header: {
      "content-type": "application/json"
    },
    success: function(c) {
      c && c.data && c.data.route && a
        .success({
          paths: c.data.route.paths
        })
    },
    fail: function(c) {
      a.fail({
        errCode: "0",
        errMsg: c.errMsg ||
          ""
      })
    }
  })
};
AMapWX.prototype.getTransitRoute = function(a) {
  var b = Object.assign({}, this.requestConfig);
  a.origin && (b.origin = a.origin);
  a.destination && (b.destination = a.destination);
  a.strategy && (b.strategy = a.strategy);
  a.city && (b.city = a.city);
  a.cityd && (b.cityd = a.cityd);
  uni.request({
    url: "https://restapi.amap.com/v3/direction/transit/integrated",
    data: b,
    method: "GET",
    header: {
      "content-type": "application/json"
    },
    success: function(c) {
      c && c.data && c.data.route && (c = c.data.route, a.success({
        distance: c.distance || "",
        taxi_cost: c.taxi_cost ||
          "",
        transits: c.transits
      }))
    },
    fail: function(c) {
      a.fail({
        errCode: "0",
        errMsg: c.errMsg || ""
      })
    }
  })
};
AMapWX.prototype.getRidingRoute = function(a) {
  var b = Object.assign({}, this.requestConfig);
  a.origin && (b.origin = a.origin);
  a.destination && (b.destination = a.destination);
  uni.request({
    url: "https://restapi.amap.com/v3/direction/riding",
    data: b,
    method: "GET",
    header: {
      "content-type": "application/json"
    },
    success: function(c) {
      c && c.data && c.data.route && a
        .success({
          paths: c.data.route.paths
        })
    },
    fail: function(c) {
      a.fail({
        errCode: "0",
        errMsg: c.errMsg ||
          ""
      })
    }
  })
};
module.exports.AMapWX = AMapWX;

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 14
    评论
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值