react-nativehuawei-mobile-serviceshuawei-developershuawei-location-kit

Huawei Location Kit for React Native, addFusedLocationEventListener method doesn't trigger callback


Setup the huawei location kit for getting device position overtime when apps in use, followed the setup from https://developer.huawei.com/consumer/en/doc/HMS-Plugin-Guides-V1/config-agc-0000001050197382-V1

we don't have real huawei device, we're using cloud debugging

Try implement to watch the gps location overtime with all these syntax

// ------ 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
}, [])

The code successfully invoke the init, requestLocationUpdatesWithCallbackEx, but console log from addFusedLocationEventListener never invoke

Already turn on hms core app permission for location, hasPermission also returned true

Tried the locationRequest options from problem with react native @hmscore/react-native-hms-location comments, still not working

How we can fix these??


Solution

  • I think it might be a usage issue. The function of addingFusedLocationEventListener is to add FusedLocationEvent Listener. This function is triggered only when FusedLocationEvent happen.

    In your description, delete removeFusedLocationEventListener after addFusedLocationEventListener, the added listener is also deleted.

    In addition, you are advised to use independent functions instead of directly defining them in input parameters.

    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 });
    };
    

    enter image description here