首页 > 解决方案 > RNLocation 包问题:subscribeToLocationUpdates 有时永远不会触发,有时它会自动开始工作

问题描述

getLatestLocation() 返回 null 并且 subscribeToLocationUpdates 永远不会触发:

import RNLocation from 'react-native-location';

RNLocation.requestPermission({
  ios: 'whenInUse',
  android: {detail: 'coarse'},
}).then((granted) => {
  if (granted) {
    RNLocation.getLatestLocation().then((loc) => {
      Alert.alert(JSON.stringify(loc)); // <--- this returns null
    });
    RNLocation.subscribeToLocationUpdates((locations) => { // <-- this never fires
      const {latitude, longitude} = locations[0];
      Alert.alert(`${JSON.stringify({latitude, longitude})}`);
    });
  }
});`

标签: javascriptreact-nativereact-native-androidpackage.json

解决方案


您可以通过将 android: 从粗略更改为精细的详细信息值来解决此问题。现在它按预期工作。

android: {detail: 'fine'},

希望这能解决你的问题


推荐阅读