Some users with Android devices complain about the issues with their device's compass. It occurs when user launch Google Maps and try to get directions, then user is prompted to calibrate your compass before you can proceed.
The basic solution is to hold the device in front of arm's length and move it around in a figure-8 pattern. The phone will collect all of the data it needs to properly calibrate the compass, so it wil not have any issues with Google Maps.
Compass calibration works by detecting the magnetic field intensity of earth. Sometimes, due to strong interference from other electronic components like speaker magnets in the phone, compass sensor may get wrong information about the magnetic poles of earth and can point in wrong direction.
In order to resolve, it prompts users to recalibrate compass by rotating in figure-8 direction so that it can check the magnetic intensity of earth in all directions. From that data, it records the actual poles of earth.
The calibration process separates the internal and external contributions to the magnetic field by checking at how the field changes as the phone is rotated. For example, in one orientation the magnetic field from the magnet in the speaker will add on to the Earth's field, but when the mobile device is rotated in the opposite direction, the two fields will partially cancels it out.
The magnetometer in the device collects the data in order to get an accurate calibration.