In view of the complex and changeable underground environment of coal mine and the long strip shape of underground roadway, a new underground location algorithm based on Stochastic Forest and environmental factor compensation is proposed. Firstly, the underground AP network model and roadway environment are analyzed, and the fingerprint localization algorithm is constructed. At the same time, the Kalman filter algorithm is used to filter the RSS signal in the offline sampling and real-time positioning stage. Then the algorithm based on random forest and environmental factor compensation is proposed. Under the assumption that the attenuation factors between the two anchor nodes are the same, the signal strength ratio compensation algorithm is proposed, which optimizes the shortcomings of the similarity of the locality error of the similar region. The target speed constraint condition is introduced to reduces errors caused by the transmission of RSS signal transmission and environmental factors. Experiments were carried out by using a bomb shelter to simulate the real mine roadway environment. The results show that the proposed algorithm can meet the high-precision positioning of the well under the conditions of sparse anchor nodes and complex environment underground.