You don't need to worry about the magnetic sensors in electric vehicles: vehicle chassis will shield your phone from fluctuations in magnetic field due to the current between the battery and the motor. But, there will be bias due to the chassis that is constant and that you will not be able to measure using inertial sensors (meaning accelerometer, gyro, compass). To make things even worse, compass is very noisy and large heading errors are common.
As for the gravity sensor, you cannot use it to observe heading: if you have a vector to the gravity center of earth you can fix two axes (pitch, roll), but not the heading (yaw).
You could use gyroscope to measure pitch, yaw and roll. Gyro measures rate of change of these three angles. Hence by integrating the samples you can, theoretically, get heading. However, this needs correct initial alignment and the errors in the sensor data are unbounded as they will be adding with each new sample. Practically speaking, your heading estimates will be easily shifted by 180 degrees within few seconds. Yes, inertial sensors in our phones are that bad.
What offers solution here is GNSS (GPS, GLONASS or GALILEO). You can use it to periodically correct the heading, but this will work only when the vehicle is moving. It will be useless at slow speeds. The integration of heading from gyroscope and from GNSS can be done optimally using Extended Kalman Filter. If you don't know what that is, trying to understand it might cause you quite a headache.
Another solution is via maps - in the 90's map-matching methods were used to enhance precision of GPS since selective availability was still at use (precision of civilian GPS was artificially degraded). You can do something similar: use map-matching to identify road on which the vehicle is traveling and then use its heading to correct your heading estimates. Note that this will not work around intersections.
Good read about inertial sensors and integrated navigation is by Groves P.D. Principles of GNSS, inertial, and multi-sensor integrated navigation systems (Artech, 2008); ISBN 1580532551
Best Answer
I will attempt to answer this question in the context of aerial electric vehicles, that is, multicopters in particular. Quadcopters maintain their position and orientation in space as a result of a tightly controlled feedback loop, which integrates measurements from accelerometers and gyroscopes (a minimum requirement), and sometimes even barometers, cameras, ultrasonic and LIDAR sensors.
In the most basic case, the Inertial Measurement Unit (IMU) is able to generate reasonably accurate measurements, which is ensured by keeping it in the middle of the vehicle, away from the electromagnetic interference of the motors, and mounted on special gyroscopic foam, to prevent vibrations of the frame from contributing to noise. After an initial calibration step on a level surface, the quadcopter is usually able to maintain a reasonably good estimate of its angular orientation and absolute acceleration. The sensors may also be shielded, or interference can be reduced by a variety of techniques, including, but not limited to the following:
However, the above concepts may easily apply to a quadcopter, whose supply voltage is of the order of about 10 Volts, with currents of up to 200 Amps. However, full size electric vehicles will have voltages of several hundred volts and currents on a similar scale.
An article here discusses how this is still an area under research. This article is as of 2011, and the scene may be totally different today.