【问题标题】:How to Navigate to Screen after GPS been enabled?启用 GPS 后如何导航到屏幕?
【发布时间】:2019-07-29 09:49:49
【问题描述】:

当用户启用 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/>
}
};

【问题讨论】:

    标签: react-native react-native-navigation


    【解决方案1】:

    如果您真的不想导航,可以使用 .then() 回调,您已经在其中记录了 success 参数。根据docsuccess是一个对象,结构如下:

    { 
        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/>
        }
    };
    

    【讨论】:

    • 对不起,这里是新手,你能给我确切的代码吗?
    • 对不起,我忘了查看您的评论,我已经更新了我的答案。
    • 嘿,我尝试在控制台中观察 react-native-android-location-services-dialog-box,但是当我尝试启用 gps 时,结果总是被禁用,我尝试了很多方法但结果仍然在控制台日志中被禁用。我认为这个包坏了
    • 并且有一个警告说,“请求位置权限时出错错误:已禁用”
    • 你检查过status 吗?
    猜你喜欢
    • 2022-01-06
    • 2018-12-30
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-01
    相关资源
    最近更新 更多