Magnetometer Tilt Compensation for Yaw Axis Using HMC5883L and BMA180

And the problem is the magnetometer need to be held flat to function properly. If we tilt it (pitch or roll) to certain angle (for example to 45 degrees) the reading will be more inaccurate the further the compass is tilted. So I need to keep the accuracy of magnetometer even we tilt it.

Thats not really correct. That is spurious wooly thinking which comes from the days of 2-axis magnetometers, maybe. A 3-axis magnetometer DOES NOT need to be held flat to function properly. It is always going to correctly show you the direction of the geomagnetic field ( or any other magnetic field which is present ), relative to its own current orientation. Apart from scaling and offset calibration for the different axial directions ( which will be an issue whether you are holding it flat and level, or not ), the assertion that the reading will be more inaccurate the further the compass is tilted, is simply not correct.

If you know the orientation of the device, you can always determine the projection of the magnetic field vector into a plane parallel to the ground. If you don't know the orientation of the device, you can compare the measured magnetic field vector to the expected magnetic field vector in your region, to determine the correct which needs to be made to the modelled orientation of the device.