【问题标题】:Is there a location settings change listener for react native?是否有用于本机反应的位置设置更改侦听器?
【发布时间】:2018-08-22 18:12:43
【问题描述】:

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

【问题讨论】:

标签: react-native geolocation


【解决方案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

【讨论】:

  • 如果我想监听位置更新,这将很有用,但事实并非如此。我只需要处理 gps 开/关事件...此外它只是 ios 的模块... :(
  • 嗨@user3470622,您找到解决方案了吗?我也面临同样的问题!
  • @user3470622 您找到解决方案了吗?即使我也面临同样的问题。
  • @SmoggeR_js 你找到解决方案了吗?即使我也面临同样的问题
  • 我面临同样的问题。
【解决方案2】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-23
    • 1970-01-01
    • 2011-04-08
    • 2020-10-12
    • 1970-01-01
    • 2020-08-27
    • 1970-01-01
    相关资源
    最近更新 更多