I'm implementing a feature in Android to point an arrow to a GPS location. According to this tutorial http://www.javacodegeeks.com/2013/09/android-compass-code-example.html . Originally, the code in the event onSensorChanged will point the Compass to the true north, now I have changed code in the event onSensorChanged to point to my GPS location like this:
@Override
public void onSensorChanged(SensorEvent event) {
// get the angle around the z-axis rotated
float degree = Math.round(event.values[0]);
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;
}
But after some test, it does not point to the correct place. Can anyone help me on this or explain what I have done wrong.
Thanks a lot.
0 comments:
Post a Comment