是否有位置设置更改侦听器为本机做出反应?

时间:2018-03-14 09:28:46

标签: react-native geolocation

我正在尝试弄清楚当用户打开或关闭设置中的位置时如何收听事件。我试过navigator.geolocation.watchposition,但没有取得多大成功,因为它没有听取相应的事件。

2 个答案:

答案 0 :(得分:1)

npm install --save react-native-location
react-native link

var React = require('react-native');
var { DeviceEventEmitter } = React;
var { RNLocation: Location } = require('NativeModules');

Location.getAuthorizationStatus(function(authorization) {
   //authorization is a string which is either "authorizedAlways",
   //"authorizedWhenInUse", "denied", "notDetermined" or "restricted"
});

Location.requestAlwaysAuthorization();
Location.startUpdatingLocation();
Location.setDistanceFilter(5.0);

var subscription = DeviceEventEmitter.addListener(
    'locationUpdated',
    (location) => {
        /* Example location returned
        {
          coords: {
            speed: -1,
            longitude: -0.1337,
            latitude: 51.50998,
            accuracy: 5,
            heading: -1,
            altitude: 0,
            altitudeAccuracy: -1
          },
          timestamp: 1446007304457.029
        }
        */
    }
);

有关详细信息,请参阅this site

答案 1 :(得分:0)

您可以使用https://www.npmjs.com/package/react-native-system-setting

 useEffect(() => {
    const locationListener = SystemSetting.addLocationListener(
      (locationEnabled) => console.log(locationEnabled),
    );

    return () => SystemSetting.removeListener(locationListener);
  }, []);