Visual-inertial odometry (VIO) often suffers from drifting, particularly in large-scale environments. Concurrently, the global navigation satellite system (GNSS) signals will be intermittent or even inaccessible in obstructed environments. In this work, we present a robust, real-time GNSS-visual-inertial state estimator, abbreviated as R2-GVIO, that achieves drift-free six-degree-of-freedom accurate global positioning in urban challenging environments. Specifically, we integrate GNSS into the optimization-based monocular and stereo VIO, enabling the fusion of GNSS real-time kinematic (RTK), reprojection error, and inertial measurement unit (IMU) preintegration within a factor graph framework. In addition, to enhance the R2-GVIO’s robustness in GNSS-unfriendly environments, we propose an online GNSS measurement outlier detection and culling algorithm based on velocity constraints and a time synchronization mechanism. Furthermore, we propose a GNSS-aided initialization method that provides accurate gravity direction, IMU zero bias, and local–global external parameters. Moreover, we adopt the marginalization strategy of covisibility graph keyframes (KFs) and add the GNSS factor constraint on the covisibility KFs pose, ensuring the real-time performance of the system. Experiments on public data sets and real-world experiments verify that in challenging environments, including urban canyons, complex indoor–outdoor, and large-scale environments, the positioning accuracy and robustness of the proposed R2-GVIO algorithm are superior to the state-of-the-art algorithms VINS-Fusion, ORB-SLAM3, IC-GVINS, and GICI-LIB. To make contribute to the community, we open sourced the complex environment data sets https://github.com/SYSU-CPNTLab/SYSU-Campus-GVI-Dataset (“SYSU-Campus-GVI” on GitHub).