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

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

概述我正在开发罗盘应用程序,我希望指南针指向特定的纬度经度位置,而不是通常的北方.我发现有一些 questions与我的问题有关,但我没有办法让他们为我工作.这是我的代码: public class MainActivity extends AppCompatActivity implements SensorEventListener { private ImageView image; 我正在开发罗盘应用程序,我希望指南针指向特定的纬度经度位置,而不是通常的北方.我发现有一些 questions与我的问题有关,但我没有办法让他们为我工作.这是我的代码:
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,0.5f);        ra.setDuration(210);        ra.setFillAfter(true);        image.startAnimation(ra);        currentDegree = -degree;    }    @OverrIDe    public voID onAccuracyChanged(Sensor sensor,int accuracy) {        // not in use    }}

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

解决方法 我想我找到了办法:
这是我已经改变以获得预期行为的方法(基于 this问题和cookieMonster答案)
@OverrIDe    public voID onSensorChanged(SensorEvent event) {        // get the angle around the z-axis rotated        float degree = Math.round(event.values[0]);        degree += geoFIEld.getDeclination();        float bearing = location.bearingTo(target);        degree = (bearing - degree) * -1;        degree = normalizeDegree(degree);        tvheading.setText("heading: " + float.toString(degree) + " degrees");        // create a rotation animation (reverse turn degree degrees)        RotateAnimation ra = new RotateAnimation(                currentDegree,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;    }private float normalizeDegree(float value) {        if (value >= 0.0f && value <= 180.0f) {            return value;        } else {            return 180 + (180 + value);        }    }
总结

以上是内存溢出为你收集整理的Android指南针指向我的位置,而不是北部全部内容,希望文章能够帮你解决Android指南针指向我的位置,而不是北部所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1132533.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存