Indoor positioning technology is one of the important supporting technologies for location services. Since WiFibased indoor positioning technology is unsuitable for scenarios where WiFi APs are sparse or missing (such as underground parking lots), magnetic fields are the only available signal source. However, magnetic positioning methods still face the problem of the high cost of magnetic map construction. This paper proposes a method for constructing a magnetic grid map of a multi-story parking lot that integrates crowdsourced vehicle and pedestrian data. This method uses the three-dimensional sparse magnetic field map obtained from vehicle data as the skeleton, performs preliminary floor allocation on pedestrian data, and combines key frame association information, floor information, and neighborhood loop information to iteratively update the graph optimization problem to achieve three-dimensional trajectory reconstruction of pedestrians and vehicles. On this basis, this method defines crowdsourced trajectory estimation and magnetic grid map generation as an optimization problem, establishes a connection between the two through multiple constraints such as trajectory relative pose constraints, magnetic field vector space consistency, and magnetic field spatial distribution continuity, and obtains the optimal estimate of magnetic grid maps and trajectories on each floor through a multi-step iterative optimization method. The effectiveness of the proposed scheme is verified by testing a simulated data set in a typical twostory underground parking lot scenario. The average positioning error of the jointly generated trajectories of pedestrians and vehicles is 1.75m, and the average positioning error based on the crowdsourced magnetic field map is 2.87m.