启用GPS后如何导航到屏幕?

时间:2019-07-29 09:49:49

标签: react-native react-native-navigation

启用用户GPS后,我想将其导航到AuthScreen.js。我使用的是react-native-navigation v1,但没有可以仅导航到简单屏幕的功能,只有推式和模式式导航,但我不想使用它。

也使用此命令:react-native-android-location-services-dialog-box

这是我的代码:

    componentDidMount() {
this.gpsLocation();
}

gpsLocation = () => {
if (Platform.OS === 'android') {
  LocationServicesDialogBox.checkLocationServicesIsEnabled({
    message: "<h2>Use Location?</h2> \
    This app wants to change your device settings:<br/><br/>\
    Use GPS for location<br/><br/>",
    ok: "Yes",
    cancel: "No",
    style: {
      backgroundColor: '#4f6d7a',
      positiveButtonTextColor: '#000000',
      negativeButtonTextColor: '#000000'
    },
    enableHighAccuracy: true,
    showDialog: true,
    openLocationServices: true,
    preventOutSideTouch: true,
    preventBackClick: true,
    providerListener: true
  }).then(function(success) {
    console.log(success)
    // return <AuthScreen/>
  }).catch((error) => {
    console.log(error.message);
  });
  };

DeviceEventEmitter.addListener('locationProviderStatusChange', function(status) {
  console.log(status);
});
};

componentWillUnmount() {
  LocationServicesDialogBox.stopListener();
};

render() {
if(!this.state.nextScreen) {
  return (
    <View style={styles.container}>
    </View>
  );
} else {
  return <AuthScreen/>
}
};

1 个答案:

答案 0 :(得分:0)

如果您真的不想导航,则可以使用.then()回调函数,其中已经在其中记录了success参数。根据{{​​3}},success是具有以下结构的对象:

{ 
    alreadyEnabled: false, 
    enabled: true, 
    status: "enabled"
}

您只需要检查success.enabled是否为true,如果是这种情况,请致电setState({ nextScreen : true });

编辑:这是要求的代码:

// MyView.js
componentDidMount()
{
    this.checkOrRequestLocationPermission();
}

checkOrRequestLocationPermission()
{
    if (Platform.OS === 'android')
    {
        LocationServicesDialogBox.checkLocationServicesIsEnabled({
            // Your config
            // ...
        })
        .then(result => {
            if (result.enabled)
            {
                this.setState({
                    nextScreen : true
                });
            }

            // The above could be replaced by this
            // this.setState({
            //     nextScreen : result.enabled
            // });
        })
        .catch(error => {
            console.warn('Error requesting location permission ' + error);
        });
    }
}

render() {
    if(!this.state.nextScreen) {
        return (
            <View style={styles.container} />
        );
    } 

    else {
        return <AuthScreen/>
    }
};