hms-react-native-plugin icon indicating copy to clipboard operation
hms-react-native-plugin copied to clipboard

华为和荣耀手机requestLocationUpdatesWithCallback报错'{"errorCode":800,"errorMessage":"10101: ARGUMENTS_INVALID"}'

Open inkCrazy opened this issue 8 months ago • 4 comments

"@hmscore/react-native-hms-location": "^6.12.0-302", "react": "18.2.0", "react-native": "0.72.4",

if (Platform.OS === 'android') {
    const locationRequest = {
      priority: HMSLocation.FusedLocation.Native.PriorityConstants.PRIORITY_HIGH_ACCURACY,
      interval: 3,
      numUpdates: 10,
      fastestInterval: 1000.0,
      expirationTime: 200000.0,
      expirationTimeDuration: 200000.0,
      smallestDisplacement: 0.0,
      maxWaitTime: 2000000.0,
      needAddress: false,
      // coordinateType: HMSLocation.Geofence.Native.GeofenceRequestConstants.COORDINATE_TYPE_GCJ_02,
    };
    const locationSettingsRequest = {
      locationRequests: [locationRequest],
      alwaysShow: false, //alwaysShow 属性指示应用程序是否需要某个位置才能继续
      needBle: false, //needBle 属性指示是否需要启用 BLE 扫描
    };

    const reqCode = 1;
    HMSLocation.FusedLocation.Native.requestLocationUpdatesWithCallback(locationRequest)
      .then(res => {
        console.log('requestLocationUpdatesWithCallback', res);
        HMSLocation.FusedLocation.Native.getLastLocation()
          .then(pos => {
            console.log('Last location:', pos);
            const coords = pos;

            // Transform coordinates
            const result = gcoord.transform(
              [coords.longitude, coords.latitude],
              gcoord.WGS84, // Source coordinate system
              targetCoordSystem, // Target coordinate system
            );
            console.log('Transformed Location:', result);

            const transformedLocation = {
              ...coords,
              longitude: result[0],
              latitude: result[1],
              originalCoords: coords,
              coordSystem: targetCoordSystem,
            };

            console.log('Transformed Location:', transformedLocation);

            // Call success callback with transformed location
            onSuccess && onSuccess(transformedLocation);
          })
          .catch(error => {
            Alert.alert('定位失败', error.message || '无法获取当前位置', [{text: '确定'}]);
            onError && onError(error);
            console.log('Failed to get last location', error);
          });
      })
      .catch(err => {
        //{"errorCode":800,"errorMessage":"10101: ARGUMENTS_INVALID"} //参数无效
        console.log('requestLocationUpdatesWithCallback error:', err.message);
      });
  }

inkCrazy avatar May 07 '25 02:05 inkCrazy

问题1: 大概解决了,但是不知道为啥

改了一下参数,好了,为啥,问的AI说【移除冲突参数(如 expirationTime),确保 fastestInterval ≤ interval ≤ maxWaitTime】

const locationRequest = {
      priority: HMSLocation.FusedLocation.Native.PriorityConstants.PRIORITY_HIGH_ACCURACY,
      interval: 10000,
      numUpdates: 2147483647,
      fastestInterval: 10000,
      expirationTime: 3372036854775807.0,
      smallestDisplacement: 0.0,
      maxWaitTime: 0,
      needAddress: false,
      language: '',
      countryCode: '',
      // coordinateType: HMSLocation.LocationKit.Native.COORDINATE_TYPE_GCJ02,
    };

inkCrazy avatar May 07 '25 02:05 inkCrazy

问题2:

https://developer.huawei.com/consumer/cn/doc/HMSCore-References/fusedlocationproviderclient-0000001050746169#section1167913136559 看文档说getLastLocation获取的缓存地址,需要调用requestLocationUpdates,我调用的requestLocationUpdatesWithCallback方法,返回的是RequestCode,然后再调用getLastLocation,requestCode都返回了,用户那边截图还是有【"Result from location kit is null.】

Image

HMSLocation.FusedLocation.Native.requestLocationUpdatesWithCallback(locationRequest)
        .then(res => {
          console.log('requestLocationUpdatesWithCallback', res); //{ requestCode: 6 }
          HMSLocation.FusedLocation.Native.getLastLocation()
            .then(pos => {
              console.log('Last location:', pos);
              const coords = pos;

              // Transform coordinates
              const result = gcoord.transform(
                [coords.longitude, coords.latitude],
                gcoord.WGS84, // Source coordinate system
                targetCoordSystem, // Target coordinate system
              );
              console.log('Transformed Location:', result);

              const transformedLocation = {
                ...coords,
                longitude: result[0],
                latitude: result[1],
                originalCoords: coords,
                coordSystem: targetCoordSystem,
              };

              console.log('Transformed Location:', transformedLocation);

              // Call success callback with transformed location
              onSuccess && onSuccess(transformedLocation);
            })
            .catch(error => {
              // Custom error handling
              Alert.alert('定位失败', error.message || '无法获取当前位置', [
                {
                  text: '取消',
                  style: 'cancel',
                },
                {
                  text: '去设置定位方式',
                  onPress: () => {
                    goLocationSetting();
                  },
                },
              ]);
              // Call error callback if provided
              onError && onError(error);
              console.log('Failed to get last location', error);
            });
        })
        .catch(error => {
          //{"errorCode":800,"errorMessage":"10101: ARGUMENTS_INVALID"} //参数无效
          console.log('requestLocationUpdatesWithCallback error:', error.message);
          Alert.alert('定位失败', error.message || '无法获取当前位置', [
            {
              text: '取消',
              style: 'cancel',
            },
            {
              text: '去设置定位方式',
              onPress: () => {
                goLocationSetting();
              },
            },
          ]);
          // Call error callback if provided
          onError && onError(error);
        });

inkCrazy avatar May 07 '25 09:05 inkCrazy

问题3: 哦哦哦,难道是我手机android14的缘故,文档支持版本:【Android 5.1 - 13(API Level 22-33)】,快点支持啊,现在很多手机都是android14,android15。 但是话又说回来:requestLocationUpdatesWithCallback和getLastLocation都可以调用啊????

我oppo手机调用requestLocationUpdates,报错了{"errorCode":800,"errorMessage":"10806: NOT_YET_SUPPORTED"},看官网描述,融合定位也没用到啥啊,非华为手机也支持的。

const requestLocationWithListener = () => {
  const locationRequest = {
    priority: HMSLocation.FusedLocation.Native.PriorityConstants.PRIORITY_HIGH_ACCURACY,
    interval: 10000,
    numUpdates: 2147483647,
    fastestInterval: 10000,
    expirationTime: 3372036854775807.0,
    smallestDisplacement: 0.0,
    maxWaitTime: 0,
    needAddress: false,
    language: '',
    countryCode: '',
    // coordinateType: HMSLocation.LocationKit.Native.COORDINATE_TYPE_GCJ02,
  };
  HMSLocation.FusedLocation.Native.requestLocationUpdates(1, locationRequest)
    .then(res => {
      console.log('requestLocationUpdates===', res);
    })
    .catch(err => {
      //oppo:{"errorCode":800,"errorMessage":"10806: NOT_YET_SUPPORTED"}
      console.log('requestLocationUpdates err', err.message);
    });
  HMSLocation.FusedLocation.Events.addFusedLocationEventListener(locationResult => {
    console.log('requestLocationUpdates===FusedLocationEventListener', locationResult);
  });
};

Image

inkCrazy avatar May 07 '25 09:05 inkCrazy

问题1: 大概解决了,但是不知道为啥

改了一下参数,好了,为啥,问的AI说【移除冲突参数(如 expirationTime),确保 fastestInterval ≤ interval ≤ maxWaitTime】

const locationRequest = {
      priority: HMSLocation.FusedLocation.Native.PriorityConstants.PRIORITY_HIGH_ACCURACY,
      interval: 10000,
      numUpdates: 2147483647,
      fastestInterval: 10000,
      expirationTime: 3372036854775807.0,
      smallestDisplacement: 0.0,
      maxWaitTime: 0,
      needAddress: false,
      language: '',
      countryCode: '',
      // coordinateType: HMSLocation.LocationKit.Native.COORDINATE_TYPE_GCJ02,
    };

有用!这库的Interface做得太差了……

eliot-ye avatar May 27 '25 02:05 eliot-ye