Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a location settings change listener for react native?

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.

like image 520
user3470622 Avatar asked Nov 05 '25 05:11

user3470622


1 Answers

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.

like image 72
David Avatar answered Nov 07 '25 09:11

David



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!