1

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??

Vina
  • 139
  • 7

1 Answers1

1

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

zhangxaochen
  • 32,744
  • 15
  • 77
  • 108
  • Updated my code, we put the `removeFusedLocationEventListener` not directly after `addFusedLocationEventListener`, we put `removeFusedLocationEventListener` inside useEffect cleanup function, even when we remove the `removeFusedLocationEventListener` it doesn't invoke the `addFusedLocationEventListener` callback too – Vina Nov 24 '21 at 07:28
  • Hi @shirley, I found that set up the locationRequest priority to 200 doesn't worked, only work with priority 100, do you know why? or how we can setup with high precision location? – Vina Nov 24 '21 at 11:16
  • hi@Vina, I think set up the locationRequest priority to 100 also can meet the requirements. According to the [docs](https://developer.huawei.com/consumer/en/doc/development/HMSCore-References/location-description-0000001088559417?ha_source=hms1), currently, the high-precision location capability of Location Kit is available only in Shenzhen, Guangzhou, Suzhou, Hangzhou, Chongqing, Chengdu, Tianjin, and Dongguan in the Chinese mainland. More cities will be supported in the near future. – zhangxaochen Nov 25 '21 at 01:16
  • Noted, thanks alot @shirley for your help :) – Vina Nov 25 '21 at 05:12