是否有用于响应本机的位置设置更改侦听器? [英] Is there a location settings change listener for react native?

查看:49
本文介绍了是否有用于响应本机的位置设置更改侦听器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying figure out how to listen to event when the user turns on or off the location in the settings. I tried navigator.geolocation.watchposition but didn't have much success, since it does not listen to the respective event.

推荐答案

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

有关更多详细信息,请参考本网站.

Refer to this site for more details.

这篇关于是否有用于响应本机的位置设置更改侦听器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆