Android Compass 指向我的位置而不是北

Posted

技术标签:

【中文标题】Android Compass 指向我的位置而不是北【英文标题】:Android Compass point to my location instead of north 【发布时间】:2016-07-28 05:04:22 【问题描述】:

我正在开发指南针应用程序,我希望指南针指向特定的纬度经度位置,而不是通常的北方。我发现了一些与我的问题相关的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,
                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
    

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

【问题讨论】:

我猜这不是您正在寻找的,但尝试接受的解决方案您可能会得到一些帮助***.com/questions/4308262/…。 【参考方案1】:

我想我找到了方法: 这是我为获得预期行为而更改的方法(基于 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,
                -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;
    

private float normalizeDegree(float value) 
        if (value >= 0.0f && value <= 180.0f) 
            return value;
         else 
            return 180 + (180 + value);
        
    

【讨论】:

以上是关于Android Compass 指向我的位置而不是北的主要内容,如果未能解决你的问题,请参考以下文章

如何根据我当前的位置使指南针指向我的特定纬度经度位置? [复制]

Android自定义指南针指向自定义位置[重复]

如何设置 MKPinAnnotation 使图像底部指向位置而不是图像中心

如何将getLocation / compass按钮放在edittext字段android studio中

Android 谷歌地图使用 GPS 定位而不是网络

位置在 android 中不断更新,而不是一次