You must calibrate the magnetometer before it will be useful as a compass. Overview here.
Pololu has simple code for implementing a tilt compensated compass with previous versions of that sensor - slight modifications may be required. Check their site for the code.