The rapid growth of traffic trajectory data and the development of positioning technology have driven the demand for its analysis. However, in the current application scenarios, there are some problems such as the deviation between positioning data and real roads and low accuracy of existing trajectory data traffic prediction models. Therefore, a map matching algorithm based on hidden Markov models is proposed in this study. The algorithm starts from the global route, selects K nearest candidate paths, and identifies candidate points through the candidate paths. It uses changes in speed, angle, and other information to generate a state transition matrix that match trajectory points to the actual route. When processing trajectory data in the experiment, K = 5 is selected as the optimal value, the algorithm takes 51 ms and the accuracy is 95.3%. The algorithm performed well in a variety of road conditions, especially in parallel and mixed road sections, with an accuracy rate of more than 96%. Although the time loss of this algorithm is slightly increased compared with the traditional algorithm, its accuracy is stable. Under different road conditions, the accuracy of the algorithm is 98.3%, 97.5%, 94.8% and 96%, respectively. The accuracy of the algorithm based on traditional hidden Markov models is 95.9%, 95.7%, 95.4% and 94.6%, respectively. It can be seen that the accuracy of the algorithm designed has higher precision. The experiment proves that the map matching algorithms based on hidden Markov models is superior to other algorithms in terms of matching accuracy. This study makes the processing of traffic trajectory data more accurate.