webyonet / react-native-android-location-services-dialog-box

React Native Android Location Services Dialog Box
MIT License
182 stars 50 forks source link

How to handle the response object in this module. If user dont give location permission then how could I again generate the location dialog box. Can you explaint the response from the module? #42

Closed Tanumay92 closed 6 years ago

webyonet commented 6 years ago
export default class TestPage extends Component {
    state = {
        position: {},
    };

    constructor(props:Object) {
        super(props);
        this.setToUserLocation().catch(error => error);
    }

    async setToUserLocation():Promise {
        let checkLocation = await this.checkLocation();

        if (!Object.is(checkLocation, 'disabled')) {
            this.getLocation();
        } else {
            this.setToUserLocation().catch(error => error); // if location services is disabled try to show dialog 
        }
    }

    async checkLocation():Promise {
        return await LocationServicesDialogBox.checkLocationServicesIsEnabled({
            message: 'Use Location ?',
            ok: 'YES',
            cancel: 'NO'
        });
    }

    getLocation():void {
        navigator.geolocation.getCurrentPosition((position) => {
                this.setState({
                    position
                });
            },
            function(error) {
                this.getLocation(); // if no location info is received try recall 
            }.bind(this),
            { enableHighAccuracy: false, timeout: 20000, maximumAge: 1000 }
        );
    }
}