The demands for indoor positioning in location-based services (LBS) and applications grow rapidly. It is beneficial for indoor positioning to combine attitude and heading information. Accurate attitude and heading estimation based on magnetic, angular rate, and gravity (MARG) sensors of micro-electro-mechanical systems (MEMS) has received increasing attention due to its high availability and independence. This paper proposes a quaternion-based adaptive cubature Kalman filter (ACKF) algorithm to estimate the attitude and heading based on smart phone-embedded MARG sensors. In this algorithm, the fading memory weighted method and the limited memory weighted method are used to adaptively correct the statistical characteristics of the nonlinear system and reduce the estimation bias of the filter. The latest step data is used as the memory window data of the limited memory weighted method. Moreover, for restraining the divergence, the filter innovation sequence is used to rectify the noise covariance measurements and system. Besides, an adaptive factor based on prediction residual construction is used to overcome the filter model error and the influence of abnormal disturbance. In the static test, compared with the Sage-Husa cubature Kalman filter (SHCKF), cubature Kalman filter (CKF), and extended Kalman filter (EKF), the mean absolute errors (MAE) of the heading pitch and roll calculated by the proposed algorithm decreased by 4–18%, 14–29%, and 61–77% respectively. In the dynamic test, compared with the above three filters, the MAE of the heading reduced by 1–8%, 2–18%, and 2–21%, and the mean of location errors decreased by 9–22%, 19–31%, and 32–54% respectively by using the proposed algorithm for three participants. Generally, the proposed algorithm can effectively improve the accuracy of heading. Moreover, it can also improve the accuracy of attitude under quasistatic conditions.