1

设置华为定位包,用于在应用程序使用时超时获取设备位置,按照https://developer.huawei.com/consumer/en/doc/HMS-Plugin-Guides-V1/config-agc-0000001050197382-V1中的设置

我们没有真正的华为设备,我们正在使用云调试

尝试使用所有这些语法来观察 gps 位置超时

// ------ Parent ------
// this put on the parent useEffect
HMSLocation.LocationKit.Native.init()
 .then(() => console.log('----------Success Initialize----------'))
 .catch((err) => alert(err.message))

// ------ Child ------
const stopWatchingLocation = () => {
  if (hasHms) {
    HMSLocation.FusedLocation.Events.removeFusedLocationEventListener(
      (res: LocationResult) => console.log('remove add listener', res),
    )
  } 
}

const startWatchingLocation = async () => {
  if (hasHms) {
      HMSLocation.FusedLocation.Native.requestLocationUpdatesWithCallbackEx(
          hwGeolocationOptions,
    )
      .then((res) => console.log('success request', res))
      .catch((error) => console.log('failed request', error))
    
    HMSLocation.FusedLocation.Events.addFusedLocationEventListener(
      (res: LocationResult) => console.log('result', res.lastHWLocation)
    )
  }
}

// implementation of add & remove event listener
useEffect(() => {
  startWatchingLocation() // inside here invoke addFusedLocationEventListener
  return stopWatchingLocation // inside here invoke, cleanup function removeFusedLocationEventListener
}, [])

代码成功调用init, requestLocationUpdatesWithCallbackEx, 但控制台日志addFusedLocationEventListener从未调用

已经开启hms core app的位置权限,hasPermission也返回true

尝试了react native @hmscore/react-native-hms-locationlocationRequest评论的问题选项,仍然无法正常工作

我们如何解决这些问题?

4

1 回答 1

1

我认为这可能是一个使用问题。的作用addingFusedLocationEventListener是添加FusedLocationEventListener。该函数仅在 FusedLocationEvent 发生时触发。

在您的描述中, delete removeFusedLocationEventListenerafter addFusedLocationEventListener,添加的侦听器也被删除。

另外,建议您使用独立的函数,而不是直接在输入参数中定义它们。

handleLocationUpdate = (locationResult) => { console.log(locationResult); this.setState({ locationCallbackResult: locationResult }); }

requestLocationCallbackWithListener = () => {
  HMSLocation.FusedLocation.Native.requestLocationUpdatesWithCallbackEx(locationRequest)
    .then((res) => this.setState({ reqCode: res.requestCode }))
    .catch((err) => alert(err.message));
  HMSLocation.FusedLocation.Events.addFusedLocationEventListener(this.handleLocationUpdate);
  this.setState({ autoUpdateEnabled: true });
};

在此处输入图像描述

于 2021-11-24T07:20:30.407 回答