With the popularity of smartphones and the development of microelectromechanical system (MEMS), the pedestrian dead reckoning (PDR) algorithm based on the built-in sensors of a smartphone has attracted much research. Heading estimation is the key to obtaining reliable position information. Hence, an adaptive Kalman filter (AKF) based on an autoregressive model (AR) is proposed to improve the accuracy of heading for pedestrian dead reckoning in a complex indoor environment. Our approach uses an autoregressive model to build a Kalman filter (KF), and the heading is calculated by the gyroscope, obtained by the magnetometer, and stored by previous estimates, then are fused to determine the measurement heading. An AKF based on the innovation sequence is used to adaptively adjust the state variance matrix to enhance the accuracy of the heading estimation. In order to suppress the drift of the gyroscope, the heading calculated by the AKF is used to correct the heading calculated by the outputs of the gyroscope if a quasi-static magnetic field is detected. Data were collected using two smartphones. These experiments showed that the average error of two-dimensional (2D) position estimation obtained by the proposed algorithm is reduced by 40.00% and 66.39%, and the root mean square (RMS) is improved by 43.87% and 66.79%, respectively, for these two smartphones.