我试图制作一个指向特定位置的指南针
我已经按照一些教程并在此处检查了之前的问题,但我没有得到正确的direction
到目标
这是我的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.compass_view);
compassIcon = (ImageView) findViewById(R.id.imageViewCompass);
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
accelerometer = sensorManager
.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
magnetometer = sensorManager
.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
myLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
currrentlocation = myLocationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (currrentlocation != null
&& currrentlocation.getTime() > Calendar.getInstance()
.getTimeInMillis() - 2 * 60 * 1000) {
// get latitude and longitude values
currentLatitude = currrentlocation.getLatitude();
currentLongitude = currrentlocation.getLongitude();
Log.d("myLocation at first ", currentLatitude + " "
+ currentLongitude);
} else {
myLocationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 1, 1, this);
}
// target location
QiblaLocation = new Location("QiblaLocation");
QiblaLocation.setLatitude(QiblaLatitude);
QiblaLocation.setLongitude(QiblaLongitude);
}
@Override
protected void onResume() {
super.onResume();
// for the system's orientation sensor registered listeners
sensorManager.registerListener(this, accelerometer,
SensorManager.SENSOR_DELAY_UI);
sensorManager.registerListener(this, magnetometer,
SensorManager.SENSOR_DELAY_UI);
}
@Override
protected void onPause() {
super.onPause();
sensorManager.unregisterListener(this);
}
@Override
public void onSensorChanged(SensorEvent event) {
// if (currrentlocation == null)
// return;
float[] mGravity = new float[3];
float[] mGeomagnetic = new float[3];
final float alpha = 0.97f;
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
mGravity[0] = alpha * mGravity[0] + (1 - alpha)
* event.values[0];
mGravity[1] = alpha * mGravity[1] + (1 - alpha) * event.values[1];
mGravity[2] = alpha * mGravity[2] + (1 - alpha) * event.values[2];
if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
mGeomagnetic[0] = alpha * mGeomagnetic[0] + (1 - alpha)
* event.values[0];
mGeomagnetic[1] = alpha * mGeomagnetic[1] + (1 - alpha)
* event.values[1];
mGeomagnetic[2] = alpha * mGeomagnetic[2] + (1 - alpha)
* event.values[2];
float R[] = new float[9];
float I[] = new float[9];
if (mGravity != null && mGeomagnetic != null) {
boolean success = SensorManager.getRotationMatrix(R, I,
mGravity, mGeomagnetic);
if (success) {
float orientation[] = new float[3];
SensorManager.getOrientation(R, orientation);
azimuth = (float) Math.toDegrees(orientation[0]);
// Log.d("first azimuth value"+String.valueOf(azimuth),null);
calculateLocation();
}
}
}
public void calculateLocation() {
GeomagneticField geoField = new GeomagneticField(Double.valueOf(
currrentlocation.getLatitude()).floatValue(), Double.valueOf(
currrentlocation.getLongitude()).floatValue(), Double.valueOf(
currrentlocation.getAltitude()).floatValue(),
System.currentTimeMillis());
azimuth -= geoField.getDeclination();
Log.d("second azimuth", String.valueOf(azimuth));
float bearTo = currrentlocation.bearingTo(QiblaLocation);
if (bearTo < 0) {
bearTo = bearTo + 360;
}
// point it
float direction = bearTo - azimuth;
if (direction < 0) {
direction = direction + 360;
}
tvHeading.setText("Heading " + Float.toString(direction));
RotateAnimation ra = new RotateAnimation(currentDegree, direction,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
currentDegree = direction;
ra.setDuration(1000);
ra.setFillAfter(true);
compassIcon.startAnimation(ra);
}
@Override
public void onLocationChanged(Location location) {
currentLatitude = location.getLatitude();
currentLongitude = location.getLongitude();
location.setLatitude(currentLatitude);
location.setLongitude(currentLongitude);
}