Android指南针指向我的位置,而不是北部

前端之家收集整理的这篇文章主要介绍了Android指南针指向我的位置,而不是北部前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在开发罗盘应用程序,我希望指南针指向特定的纬度经度位置,而不是通常的北方.我发现有一些 questions与我的问题有关,但我没有办法让他们为我工作.这是我的代码
  1. public class MainActivity extends AppCompatActivity implements SensorEventListener {
  2. private ImageView image;
  3. private float currentDegree = 0f;
  4. private SensorManager mSensorManager;
  5. private TextView tvHeading;
  6. private Location location = new Location("A");
  7. private Location target = new Location("B");
  8. private LocationManager locationManager;
  9.  
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.activity_main);
  14.  
  15. image = (ImageView) findViewById(R.id.imageViewCompass);
  16. tvHeading = (TextView) findViewById(R.id.tvHeading);
  17.  
  18. mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
  19.  
  20. location.setLatitude(54.903535);
  21. location.setLongitude(23.979342);
  22.  
  23. target.setLatitude(54.904618);
  24. target.setLongitude(23.978782);
  25. }
  26.  
  27. @Override
  28. protected void onResume() {
  29. super.onResume();
  30.  
  31. mSensorManager.registerListener(this,mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),SensorManager.SENSOR_DELAY_GAME);
  32. }
  33.  
  34. @Override
  35. protected void onPause() {
  36. super.onPause();
  37. mSensorManager.unregisterListener(this); // to stop the listener and save battery
  38. }
  39.  
  40. @Override
  41. public void onSensorChanged(SensorEvent event) {
  42. float degree = Math.round(event.values[0]);
  43. tvHeading.setText("Heading: " + Float.toString(degree) + " degrees");
  44. RotateAnimation ra = new RotateAnimation(
  45. currentDegree,-degree,Animation.RELATIVE_TO_SELF,0.5f,0.5f);
  46. ra.setDuration(210);
  47. ra.setFillAfter(true);
  48.  
  49. image.startAnimation(ra);
  50. currentDegree = -degree;
  51. }
  52.  
  53. @Override
  54. public void onAccuracyChanged(Sensor sensor,int accuracy) {
  55. // not in use
  56. }
  57. }

目前我的指针只显示在北方,而不是我的编码目标位置.

解决方法

我想我找到了办法:
这是我已经改变以获得预期行为的方法(基于 this问题和CookieMonster答案)
  1. @Override
  2. public void onSensorChanged(SensorEvent event) {
  3. // get the angle around the z-axis rotated
  4. float degree = Math.round(event.values[0]);
  5. degree += geoField.getDeclination();
  6.  
  7. float bearing = location.bearingTo(target);
  8. degree = (bearing - degree) * -1;
  9. degree = normalizeDegree(degree);
  10.  
  11. tvHeading.setText("Heading: " + Float.toString(degree) + " degrees");
  12.  
  13. // create a rotation animation (reverse turn degree degrees)
  14. RotateAnimation ra = new RotateAnimation(
  15. currentDegree,0.5f);
  16.  
  17. // how long the animation will take place
  18. ra.setDuration(210);
  19.  
  20. // set the animation after the end of the reservation status
  21. ra.setFillAfter(true);
  22.  
  23. // Start the animation
  24. image.startAnimation(ra);
  25. currentDegree = -degree;
  26. }
  27.  
  28. private float normalizeDegree(float value) {
  29. if (value >= 0.0f && value <= 180.0f) {
  30. return value;
  31. } else {
  32. return 180 + (180 + value);
  33. }
  34. }

猜你在找的Android相关文章