uiwjs / react-native-amap-geolocation

React Native 高德地图定位模块,支持 Android/iOS。
https://uiwjs.github.io/react-native-amap-geolocation/
MIT License
42 stars 5 forks source link

IOS真机调试必须reloading app... 后才能获取定位信息 #35

Closed xiaobc1234 closed 1 year ago

xiaobc1234 commented 1 year ago

初始化的代码放在 useEffect里,其他地方没有 AMapGeolocation 类的引入和使用。在xcode本地启动后会有如下报错, 但此时app还在lunach阶段, 并且后面定位一直不会获取。 image

如下操作 刷新app之后就能开始正常获取定位信息 image


useEffect(() => {
  amapLocationListener(user);
}, [])

// 高德地图持续定位
function amapLocationListener(user) {
  let apiKey = ""
  if (Platform.OS === "ios") {
    apiKey = Config.AMAP_KEY_IOS
  }
  if (Platform.OS === "android") {
    apiKey = Config.AMAP_KEY_ANDROID
  }
  if (apiKey) {
    try {
      // 设置 高德地图 apiKey
      AMapGeolocation.setApiKey(apiKey)
    } catch (error) {
      console.log("error:", error)
    }
  }
  console.log("apiKey: ", apiKey)

  // iOS 指定所需的精度级别
  AMapGeolocation.setDesiredAccuracy(3)
  // Android 指定所需的精度级别,可选设置,默认 高精度定位模式
  AMapGeolocation.setLocationMode(1)
  // 定位是否返回逆地理信息, 这里只要定位
  AMapGeolocation.setLocatingWithReGeocode(false)
  // 定位上报间隔
  AMapGeolocation.setInterval(3000)
  // 当设备可以正常联网时,还可以返回该定位点的对应的中国境内位置信息(包括:省、市、区/县以及详细地址)。
  const listener = AMapGeolocation.addLocationListener((location) => {
    console.log("返回定位信息", location, user)
    locationReport(
      {
        coords: {
          longitude: location.longitude,
          latitude: location.latitude,
          speed: location.speed,
        },
        timestamp: location.timestamp,
      },
      user,
      "GCJ-02", // 用高德sdk获取的定位 使用的高德的坐标系
    )
  })

  // 开启监听
  AMapGeolocation.start()

  return () => {
    // 停止更新位置信息
    AMapGeolocation.stop()
    // 移除监听事件
    listener && listener.remove()
  }
}
xiaobc1234 commented 1 year ago

"react-native": "0.67.2"

xiaobc1234 commented 1 year ago

image

还在打包的时候就报了 apiKey无效的错

xiaobc1234 commented 1 year ago

image

我把AMapGeolocation的引入都注释掉,重新clean再打包调试,依然报上面的错 INVALID_USER_KEY

jaywcjlove commented 1 year ago

这个我之前也发现了,我重新申请了 apikey 没有这个错误了

官方客户说,地图报 INVALID_USER_KEY 错误是,“key不正确或过期,开发者发起请求时,传入的key不正确或者过期”

@xiaobc1234

xiaobc1234 commented 1 year ago

我自己的key和公司给的key都是昨天刚生成的,两个key在热更新一次后都能正常获取定位

看上去并不是key不对,因为热更新后能正常定位。而且报这个错的时候app还在加载阶段。 @jaywcjlove

xiaobc1234 commented 1 year ago

"@uiw/react-native-amap-geolocation": "2.0.0-alpha.2" 依赖包版本是2.0.0-alpha.2

jaywcjlove commented 1 year ago

@xiaobc1234 你看看它就 6行代码的实现,你可以咨询一下高德地图的客服,我也是咨询客服,换了 key 然后就好了。

image

https://github.com/uiwjs/react-native-amap-geolocation/blob/55e502cfefc63bb00cb7b408997e26336496aa5d/index.js#L13-L15

https://github.com/uiwjs/react-native-amap-geolocation/blob/55e502cfefc63bb00cb7b408997e26336496aa5d/ios/RNAMapGeolocation.m#L45-L47

jaywcjlove commented 1 year ago

请确保在 SDK 任何类的初始化以及方法调用之前设置正确的 Key。

已经找到问题,可能是这个 @xiaobc1234

xiaobc1234 commented 1 year ago

感觉是你的包里 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()
  }
}
jaywcjlove commented 1 year ago

@xiaobc1234 已经修复