是否有用于反应本机的位置设置更改侦听器?

2024-04-10

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


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
        }
        */
    }
);

参考这个网站 https://github.com/timfpark/react-native-location更多细节。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

是否有用于反应本机的位置设置更改侦听器? 的相关文章

随机推荐