Closed xiaobc1234 closed 1 year ago
"react-native": "0.67.2"
还在打包的时候就报了 apiKey无效的错
我把AMapGeolocation的引入都注释掉,重新clean再打包调试,依然报上面的错 INVALID_USER_KEY
这个我之前也发现了,我重新申请了 apikey
没有这个错误了
官方客户说,地图报 INVALID_USER_KEY 错误是,“key不正确或过期,开发者发起请求时,传入的key不正确或者过期”
@xiaobc1234
我自己的key和公司给的key都是昨天刚生成的,两个key在热更新一次后都能正常获取定位
看上去并不是key不对,因为热更新后能正常定位。而且报这个错的时候app还在加载阶段。 @jaywcjlove
"@uiw/react-native-amap-geolocation": "2.0.0-alpha.2" 依赖包版本是2.0.0-alpha.2
@xiaobc1234 你看看它就 6行代码的实现,你可以咨询一下高德地图的客服,我也是咨询客服,换了 key 然后就好了。
请确保在 SDK 任何类的初始化以及方法调用之前设置正确的 Key。
已经找到问题,可能是这个 @xiaobc1234
感觉是你的包里 init方法调用时机问题,我换成 react-native-amap-geolocation 这个库之后 就正常了。
import { DeviceEventEmitter } from "react-native"
import Config from "react-native-config"
import debounce from "lodash/debounce"
import {
init,
setDesiredAccuracy,
setLocationMode,
setLocatingWithReGeocode,
setAllowsBackgroundLocationUpdates,
setInterval,
addLocationListener,
start,
stop,
setDistanceFilter,
Geolocation,
} from "react-native-amap-geolocation"
import { traceReport, COORD_TYPE } from "Services/api"
export async function locationReport(position, user, coordType) {
const res = await traceReport(
{
driverAccount: user.loginId, // 账户 必填
lon: position.coords.longitude, // 经度 必填
lat: position.coords.latitude, // 纬度 必填
speed: position.coords.speed, // 速度
time: position.timestamp, // 时间 必填
},
coordType,
)
// 触发定位缓存更新
DeviceEventEmitter.emit("refreshLocationCache", {
latitude: position.coords.latitude,
longitude: position.coords.longitude,
coordType: coordType || COORD_TYPE,
})
}
export async function amapLocationListener(user) {
await init({
ios: Config.AMAP_KEY_IOS,
android: Config.AMAP_KEY_ANDROID,
})
// iOS 指定所需的精度级别
setDesiredAccuracy(3)
// Android 指定所需的精度级别,可选设置,默认 高精度定位模式
setLocationMode(1)
// 定位是否返回逆地理信息, 这里只要定位
setLocatingWithReGeocode(false)
// 允许后台运行
setAllowsBackgroundLocationUpdates(true)
// android 定位上报间隔
setInterval(1000 * 60)
// ios,设备移动超过 10 米才会更新位置信息
setDistanceFilter(10)
// 当设备可以正常联网时,还可以返回该定位点的对应的中国境内位置信息(包括:省、市、区/县以及详细地址)。
const listener = addLocationListener((location) => {
console.log("返回定位信息", location, user)
locationReport(
{
coords: {
longitude: location.longitude,
latitude: location.latitude,
speed: location.speed,
},
timestamp: location.timestamp,
},
user,
"GCJ-02", // 用高德sdk获取的定位 使用的高德的坐标系
)
})
// 开启监听
start()
return () => {
console.log("destroy...")
// 停止更新位置信息
stop()
// 移除监听事件
listener && listener.remove()
}
}
@xiaobc1234 已经修复
初始化的代码放在 useEffect里,其他地方没有 AMapGeolocation 类的引入和使用。在xcode本地启动后会有如下报错, 但此时app还在lunach阶段, 并且后面定位一直不会获取。
如下操作 刷新app之后就能开始正常获取定位信息