10
我正在開發指南針應用程序,我希望指南針指向特定的緯度經度位置而不是通常的北部。我發現一些questions與我的問題有關,但我無法讓他們爲我工作。這裏是我的代碼:Android指南針指向我的位置而不是北部
public class MainActivity extends AppCompatActivity implements SensorEventListener {
private ImageView image;
private float currentDegree = 0f;
private SensorManager mSensorManager;
private TextView tvHeading;
private Location location = new Location("A");
private Location target = new Location("B");
private LocationManager locationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image = (ImageView) findViewById(R.id.imageViewCompass);
tvHeading = (TextView) findViewById(R.id.tvHeading);
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
location.setLatitude(54.903535);
location.setLongitude(23.979342);
target.setLatitude(54.904618);
target.setLongitude(23.978782);
}
@Override
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
SensorManager.SENSOR_DELAY_GAME);
}
@Override
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this); // to stop the listener and save battery
}
@Override
public void onSensorChanged(SensorEvent event) {
float degree = Math.round(event.values[0]);
tvHeading.setText("Heading: " + Float.toString(degree) + " degrees");
RotateAnimation ra = new RotateAnimation(
currentDegree,
-degree,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF,
0.5f);
ra.setDuration(210);
ra.setFillAfter(true);
image.startAnimation(ra);
currentDegree = -degree;
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// not in use
}
}
目前我的指針只顯示在北而不是我的編碼目標位置。
我想這不正是你正在尋找但嘗試接受的解決方案,你可能會得到一些幫助http://stackoverflow.com/questions/4308262/calculate-compass-bearing-heading-to-location-in-android。 – Raghavendra