首先到达当前位置的北面, 然后计算轴承,
这是代码:
@Override
public void onSensorChanged(SensorEvent event) {
float azimuth = (int)event.values[0];
azimuth = azimuth * 180 / (float) Math.PI;
GeomagneticField geoField = new GeomagneticField(Double.valueOf(
location.getLatitude()).floatValue(), Double.valueOf(
location.getLongitude()).floatValue(), Double.valueOf(
location.getAltitude()).floatValue(),
System.currentTimeMillis());
azimuth += geoField.getDeclination(); // converts magnetic north
// into true north
float bearing = location.bearingTo(MeccaLocation); // (it's already
// in degrees)
float Meccadirection = (float) (azimuth - bearing);
}
但结果方向不准确。