Magnetometers provide compass information, and are widely used for navigation, orientation and alignment of objects. As magnetometers are affected by sensor biases and eventually by systematic distortions of the Earth magnetic field, a calibration is needed. In this paper, a method for calibration of magnetometers with three Global Navigation Satellite System (GNSS) receivers is presented. We perform a least-squares estimation of the magnetic flux and sensor biases using GNSS-based attitude information. The attitude is obtained from the relative positions between the GNSS receivers in the North-East-Down coordinate frame and prior knowledge of these relative positions in the platform's coordinate frame. The relative positions and integer ambiguities of the periodic carrier phase measurements are determined with an integer least-squares estimation using an integer decorrelation and sequential tree search. Prior knowledge on the relative positions is used to increase the success rate of ambiguity fixing. We have validated the proposed method with low-cost magnetometers and GNSS receivers on a vehicle in a test drive. The calibration enabled a consistent heading determination with an accuracy of five degrees. This precise magnetometer-based attitude information allows an instantaneous GNSS integer ambiguity fixing.