<p>Provided is a two-axis magnetic field sensor having a function to generate a magnetic field to cancel an external offset magnetic field. The sensor includes a planar coil arranged in a plane and four pairs of magnetoresistive elements arranged in a plane parallel to the planar coil. The planar coil has at least two sets of conductors parallel to each other and the two of the magnetoresistive elements intersect only one side of the coil. A current to cancel the external offset magnetic field is decided in advance. While a DC current generating a total magnetic field of a bias magnetic field and a magnetic field to cancel the external offset magnetic field is flowing into the coil, an intermediate potential output of the magnetoresistive element pair is extracted to decide a magnetic azimuth of geomagnetism or the like.</p>