【发布时间】:2018-08-22 18:12:43
【问题描述】:
我正在尝试弄清楚当用户在设置中打开或关闭位置时如何监听事件。我尝试了navigator.geolocation.watchposition,但没有多大成功,因为它不监听相应的事件。
【问题讨论】:
-
不,因为它不会在用户打开或关闭 GPS 时监听事件,它只监听位置变化事件
-
嗨,我想你在找github.com/rmrs/…
我正在尝试弄清楚当用户在设置中打开或关闭位置时如何监听事件。我尝试了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
}
*/
}
);
详情请参阅this site。
【讨论】:
您可以使用https://www.npmjs.com/package/react-native-system-setting
useEffect(() => {
const locationListener = SystemSetting.addLocationListener(
(locationEnabled) => console.log(locationEnabled),
);
return () => SystemSetting.removeListener(locationListener);
}, []);
【讨论】: