【问题标题】:app crash when using Geolocation in React-Native在 React-Native 中使用 Geolocation 时应用程序崩溃
【发布时间】:2022-10-04 13:52:31
【问题描述】:

我正在使用react-native-geolocation-service,我有这样的功能来获取坐标。然后通过 Geocoder api 通过坐标获取城市。我通过react-native-permissions查看权限状态:

export const getCity = async (dispatch: Dispatch<any>) => {
  const granted = await check(PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION);

  if (granted === RESULTS.DENIED) {
    await PermissionsAndroid.request(PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION, {
      buttonNegative: undefined,
      buttonNeutral: undefined,
      buttonPositive: '',
      title: 'Access',
      message: 'give access',
    });
  }

  if (granted === RESULTS.GRANTED || granted === RESULTS.LIMITED) {
    Geolocation.getCurrentPosition(
      position => {
        const coords: Coords = {
          Longitude: position.coords.longitude,
          Latitude: position.coords.latitude,
        };
        dispatch(thunks.geocoder.getGeocoderInfo(coords));
        Alert.alert('coords', `${coords.Latitude}, ${coords.Longitude}`);
      },
      error => Alert.alert('Error', `Error ${error.message}`),
      {
        enableHighAccuracy: false,
        timeout: 20000,
        maximumAge: 3600000,
      },
    );
  }
};

因此,当我的应用程序启动时,我授予它权限,然后它就关闭了。我还添加了访问AndroidManifest.xml:&lt;uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/&gt;。然后我使用UseEffect() 在我的组件中调用这个函数

【问题讨论】:

  • 将手机插入计算机,打开 Android Studio 并再次运行您的应用。在 LogCat 窗口中,您应该会看到导致应用崩溃的错误。您可以使用该错误更新您的问题,然后人们会更容易帮助您
  • @CarlosJ Tanks 供您发表评论。我会尝试

标签: react-native geolocation


【解决方案1】:

我解决了我的问题。我重写我的代码表单问题如下:


type Coords = {
  coords: {
    accuracy?: number | null;
    altitude?: number | null;
    altitudeAccuracy?: number | null;
    heading?: number | null;
    latitude?: number | null;
    longitude?: number | null;
    speed?: number | null;
  };
  mocked?: boolean;
  provider?: string;
  timestamp?: number | null;
};

export const getCity = async (dispatch: Dispatch<any>) => {
  const granted = await check(PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION);
  let geoOptions = {
    enableHighAccuracy: true,
    timeOut: 20000,
    maximumAge: 60 * 60 * 24,
  };
  const geoSuccess = (position: Coords) => {
    const coords = {
      Latitude: position.coords.latitude,
      Longitude: position.coords.longitude,
    };

    dispatch(thunks.geocoder.getGeocoderInfo(coords));
  };

  const geoFailure = (err: any) => {
    console.log({error: err.message});
  };

  if (granted === RESULTS.DENIED) {
    const data = await PermissionsAndroid.request(
      PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION,
      {
        buttonNegative: undefined,
        buttonNeutral: undefined,
        buttonPositive: '',
        title: 'Access',
        message: 'give access',
      },
    );
  }

  if (granted === RESULTS.GRANTED || granted === RESULTS.LIMITED) {
    Geolocation.getCurrentPosition(geoSuccess, geoFailure, geoOptions);
  }
};

所以它解决了我的问题。应用程序现在不会粉碎。

【讨论】:

    【解决方案2】:

    这个问题可以通过将你的 react native 升级到 0.70.1 来解决,我刚刚解决了这个问题,现在 gelocaiton 服务在调试和发布 apk 中都可以正常工作。 并且不要忘记在 androidmanifest.xml 中添加使用权限

    【讨论】:

      猜你喜欢
      • 2019-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多