收藏
回答

getLocation errCode: 2 获取地理位置失败?

在获取定位信息的时候,api报错,麻烦帮看一下是什么问题??
const authorizeLocation = () => {
    if (location?.latitude && location?.longitude) return; // 当前已经设置了
    wx.getSetting().then(async (res) => {

      if (res.errMsg === "getSetting:ok") {
        // 如果没有授权获取地理位置
        let hasAuthrized = 'scope.userLocation' in res.authSetting; // 说明已经授过权了 (有可能中途取消);
        let authrized = !!res.authSetting['scope.userLocation'];

        // 从未授权过 -> 去授权
        if (!hasAuthrized) {
          try {
            const authRes = await Taro.authorize({
              scope: "scope.userLocation",
            });
            authrized = authRes.errMsg === "authorize:ok";
          } catch (err) {
            authrized = false;
          }
        }

        // 拒绝授权
        if (!authrized) {
          return Taro.showModal({
            title: 'xxxx',
            content: 'xxxx',
            cancelText: '取消',
            confirmText: '前往授权',
            success: (r) => {
              if (r.confirm) {
                Taro.openSetting({
                  success: (setting) => {
                    // 判断获取地理位置授权是否变化
                    // 变化则重新走授权的流程
                    if (setting.authSetting['scope.userLocation']) {
                      authorizeLocation();
                    }
                  }
                });
              }
            }
          })
        }

        // 授权通过 去获取地理位置
        try {
          const l = await Taro.getLocation({ type: 'gcj02', isHighAccuracy: true });
          setLocation(l);
        } catch (err) {
          console.log(err);
        }

        const startRes: any = await Taro.startLocationUpdate()
        if (startRes.errMsg === 'startLocationUpdate:ok') {
          Taro.offLocationChange(setLocation as any);
          Taro.onLocationChange(setLocation);
        }
      } else {
        console.error(res);
      }
    });
  }


设备信息等:

错误信息 getLocation:fail:ERROR_NOCELL&WIFI_LOCATIONSWITCHOFF

回答关注问题邀请回答
收藏

2 个回答

  • 追风
    追风
    2022-05-18

    目前关注这个问题,可能出现的原因总结一下:

    1、相关权限是否已开启,打印systemInfo查看一下手机的定位等权限是否开启;

    2、微信分身应用;

    3、电信基站定位?

    2022-05-18
    有用
    回复
  • 柒
    2022-05-15

    官方改政策了,你要去公众平台开发里面申请才能使用~

    2022-05-15
    有用
    回复 1
    • 追风
      追风
      2022-05-15
      申请都通过了的
      2022-05-15
      回复
登录 后发表内容