Heading errors caused by gyroscope drift affect the positioning precision of pedestrian dead reckoning, and these errors are even greater for smartphone-based reckoning. In this study, an optimised improved heuristic drift elimination (O-iHDE) method is proposed to correct the heading errors on a smartphone gyroscope. Based on an analysis of the improved heuristic drift elimination (iHDE) and enhanced improved heuristic drift elimination (E-iHDE) algorithms, the quaternion method is used to update the attitude and angle threshold judgement conditions, and a method for correcting the quaternion is added to eliminate the heading errors caused by random gyro errors. The analysis of multiple sets of experiments shows that the new method improves the ability to discern and correct the walking route, and the heading accuracy is improved by more than 90%, which extends the effective operation time of pedestrian dead reckoning positioning based on the step-by-step system.