我正在Android中实现一项功能,将箭头指向GPS位置。 根据本教程http://www.javacodegeeks.com/2013/09/android-compass-code-example.html。最初,onSensorChanged事件中的代码会将指南针指向真正的北方,现在我在onSensorChanged事件中更改了代码以指向我的GPS位置,如下所示:
@Override
public void onSensorChanged(SensorEvent event) {
// get the angle around the z-axis rotated
float degree = Math.round(event.values[0]);
/*----------------This is the code I added to the original code------*/
Location currentLoc = // get location from GPS or network
GeomagneticField geoField = new GeomagneticField(
(float) currentLoc.getLatitude(),
(float) currentLoc.getLongitude(),
(float) currentLoc.getAltitude(),
System.currentTimeMillis());
degree += geoField.getDeclination();
/*----------------------------------------------------------------------*/
tvHeading.setText("Heading: " + Float.toString(degree) + " degrees");
// create a rotation animation (reverse turn degree degrees)
RotateAnimation ra = new RotateAnimation(
currentDegree,
-degree,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF,
0.5f);
// how long the animation will take place
ra.setDuration(210);
// set the animation after the end of the reservation status
ra.setFillAfter(true);
// Start the animation
image.startAnimation(ra);
currentDegree = -degree;
}
但经过一些测试,它并没有指向正确的位置。任何人都可以帮我解释或解释我做错了什么。
非常感谢。