Most cell phones offer digital compass functionality which uses a 3-axis magnetometer chip for data. Phones (some more than others?) frequently require the user to "recalibrate", which involves rotating the phone in a "swirly" or other pattern to populate readings over some fraction of a sphere representing the various orientation with respect to the local field.
The discussion in this answer to Magnetometer ∞ shaped calibration as well as the 3D plots of data give an excellent overview of what is happening mathematically. Roughly speaking, six parameters - a gain and an offset for each of the three axes - are fit to a dataset collected over a cloud of data points that at cover at least a chunk of that sphere.
I've done this myself with a magnetometer attached to an Arduino and fitting offline. The low cost chip I used seemed to drift a lot (being careful to do this outdoors and away from more obvious sources of error or gradients like nearby ferrous materials or other possible fields). I noticed that the offsets seemed to be contributing as much to the drift as the gains, if not more.
Question: What exactly is drifting within the magnetometer sensors in newer, high-end cell phones that still requires frequent recalibration? Is it simply temperature? If so, why wouldn't the magnetometers be internally temperature stabilized? Is it too hard to do in analog and the magnetometers don't have any ability to calculate offsets?
above: After paying a zillion dollars for a very nice phone, I feel like an idiot doing this (from support.google.com/maps) while balancing on a bicycle on a busy street corner, or enjoying some beautiful view on a hike. My iPhone makes me go through a much longer, silly video-game-like task before it will even begin to tell me anything useful. I cary a normal compass sometimes because it just simply works.
above: Screen shot from this nice discussion of the principles and mathematics behind magnetometer based compass calibrations. Here is another.