赞
踩
1.开通授权 2.配置app.config
关键句
requiredPrivateInfos: ["getLocation", "chooseAddress","chooseLocation"],
- <View
- className={styles.item_title_right}
- onClick={getUserPosition}
- >
- 获取当前位置
- </View>
- /**
- * 获取用户当前信息
- */
- const getUserPosition = () => {
- Taro.chooseLocation({
- success: function (res) {
- console.log("res", res);
-
- setForm(pre => {
- return {
- ...pre,
- position: res.address
- };
- });
- },
- fail: function () {
- Taro.getSetting({
- success: function (res) {
- var statu = res.authSetting;
- if (!statu["scope.userLocation"]) {
- Taro.showModal({
- title: "是否授权当前位置",
- content:
- "需要获取您的地理位置,请确认授权,否则地图功能将无法使用",
- success: function (tip) {
- if (tip.confirm) {
- Taro.openSetting({
- success: function (data) {
- if (data.authSetting["scope.userLocation"] === true) {
- Taro.showToast({
- title: "授权成功",
- icon: "success",
- duration: 1000
- });
- //授权成功之后,再调用chooseLocation选择地方
- Taro.chooseLocation({
- success: function (ress) {
- setForm(pre => {
- return {
- ...pre,
- position: ress.position
- };
- });
- }
- });
- } else {
- Taro.showToast({
- title: "授权失败",
- icon: "none",
- duration: 1000
- });
- }
- }
- });
- }
- }
- });
- }
- },
- fail: function (res) {
- Taro.showToast({
- title: "调用授权窗口失败",
- icon: "success",
- duration: 1000
- });
- }
- });
- }
- });
- };
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。