getLocation:fail the api need to be declared in the requiredPrivateInfos field in app.json/ext.json
微信小程序在使用wx.getLocation控制台报错:getLocation:fail the api need to be declared in the requiredPrivateInfos field in app.json/ext.json
wx.getLocation({ type: 'wgs84', success: res => { const latitude = res.latitude; const longitude = res.longitude; console.log('用户位置:', latitude, longitude); }, fail: err => { console.error('获取用户位置失败:', err); } });
.
原因 在小程序的 app.json 或者 ext.json 文件中,有一个 requiredPrivateInfos 字段用于声明需要使用的私有接口。这些私有接口需要在此字段中进行声明。
解决方案:
在app.json内插入一下代码
"requiredPrivateInfos":["getLocation"],