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

发布于 2024-12-08 19:43:01 字数 130 浏览 0 评论 0原文

我正在开发一个 Android 应用程序,我希望指南针指向特定的纬度经度位置,而不是通常的北位置。任何人都可以建议如何实现这一点。我开发了一个指向北方向的应用程序。
预先感谢如何实施这一点。任何执行此操作的演示应用程序都会有很大帮助。

I'm developing an android application where I want the compass to point to a particular latitude longitude position instead of the usual north location. Can anyone suggest how to implement this. I've developed an application which points to north direction.
Thanks in advance in how to implement this. Any demo application which does this would be of great help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

⊕婉儿 2024-12-15 19:43:01

您可以从此处实现简单的指南针。
它显然指向北方,但为了使指南针指向某个位置或坐标,你可以做这样的事情。

在您的活动中添加此方法,它会找到两个坐标之间的方位。

    protected double bearing(double startLat, double startLng, double endLat, double endLng){
       double longitude1 = startLng;
       double longitude2 = endLng;
       double latitude1 = Math.toRadians(startLat);
       double latitude2 = Math.toRadians(endLat);
       double longDiff= Math.toRadians(longitude2-longitude1);
       double y= Math.sin(longDiff)*Math.cos(latitude2);
       double x=Math.cos(latitude1)*Math.sin(latitude2)-Math.sin(latitude1)*Math.cos(latitude2)*Math.cos(longDiff);

    return (Math.toDegrees(Math.atan2(y, x))+360)%360;
}

在 Compass 类的 onSensorChanged 方法中,执行

azimuth -= bearing(yourlatitude, yourlongitude, latWhereToPoint, lngWhereToPoint);

纬度、经度 ->您当前的纬度、经度
latWhereToPoint, lngWheretoPoint ->;您想要指向的位置。

最后你的 onSensorChanged 会是这样的。

@Override
public void onSensorChanged(SensorEvent event) {
    final float alpha = 0.97f;

    synchronized (this) {
        if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {

            mGravity[0] = alpha * mGravity[0] + (1 - alpha)
                    * event.values[0];
            mGravity[1] = alpha * mGravity[1] + (1 - alpha)
                    * event.values[1];
            mGravity[2] = alpha * mGravity[2] + (1 - alpha)
                    * event.values[2];

            // mGravity = event.values;

            // Log.e(TAG, Float.toString(mGravity[0]));
        }

        if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
            // mGeomagnetic = event.values;

            mGeomagnetic[0] = alpha * mGeomagnetic[0] + (1 - alpha)
                    * event.values[0];
            mGeomagnetic[1] = alpha * mGeomagnetic[1] + (1 - alpha)
                    * event.values[1];
            mGeomagnetic[2] = alpha * mGeomagnetic[2] + (1 - alpha)
                    * event.values[2];
            // Log.e(TAG, Float.toString(event.values[0]));

        }

        float R[] = new float[9];
        float I[] = new float[9];
        boolean success = SensorManager.getRotationMatrix(R, I, mGravity,
                mGeomagnetic);
        if (success) {
            float orientation[] = new float[3];
            SensorManager.getOrientation(R, orientation);
            // Log.d(TAG, "azimuth (rad): " + azimuth);
            azimuth = (float) Math.toDegrees(orientation[0]); // orientation
            azimuth = (azimuth + 360) % 360;

            azimuth -= bearing(yourlatitude, yourlongitude, latWhereToPoint, lngWhereToPoint);
            // Log.d(TAG, "azimuth (deg): " + azimuth);
            adjustArrow();
        }
    }
}

You can implement simple compass from here.
It obviously pointing to north, but in order to make compass points to certain location or coordinates you can do some thing like this.

Add this method in your activity, it finds the bearing between two coordinates.

    protected double bearing(double startLat, double startLng, double endLat, double endLng){
       double longitude1 = startLng;
       double longitude2 = endLng;
       double latitude1 = Math.toRadians(startLat);
       double latitude2 = Math.toRadians(endLat);
       double longDiff= Math.toRadians(longitude2-longitude1);
       double y= Math.sin(longDiff)*Math.cos(latitude2);
       double x=Math.cos(latitude1)*Math.sin(latitude2)-Math.sin(latitude1)*Math.cos(latitude2)*Math.cos(longDiff);

    return (Math.toDegrees(Math.atan2(y, x))+360)%360;
}

In onSensorChanged Method in Compass Class, do this

azimuth -= bearing(yourlatitude, yourlongitude, latWhereToPoint, lngWhereToPoint);

latitude, longitude -> your current latitude, longitude
latWhereToPoint, lngWheretoPoint -> location where you wants to point.

In the End your onSensorChanged would be like this.

@Override
public void onSensorChanged(SensorEvent event) {
    final float alpha = 0.97f;

    synchronized (this) {
        if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {

            mGravity[0] = alpha * mGravity[0] + (1 - alpha)
                    * event.values[0];
            mGravity[1] = alpha * mGravity[1] + (1 - alpha)
                    * event.values[1];
            mGravity[2] = alpha * mGravity[2] + (1 - alpha)
                    * event.values[2];

            // mGravity = event.values;

            // Log.e(TAG, Float.toString(mGravity[0]));
        }

        if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
            // mGeomagnetic = event.values;

            mGeomagnetic[0] = alpha * mGeomagnetic[0] + (1 - alpha)
                    * event.values[0];
            mGeomagnetic[1] = alpha * mGeomagnetic[1] + (1 - alpha)
                    * event.values[1];
            mGeomagnetic[2] = alpha * mGeomagnetic[2] + (1 - alpha)
                    * event.values[2];
            // Log.e(TAG, Float.toString(event.values[0]));

        }

        float R[] = new float[9];
        float I[] = new float[9];
        boolean success = SensorManager.getRotationMatrix(R, I, mGravity,
                mGeomagnetic);
        if (success) {
            float orientation[] = new float[3];
            SensorManager.getOrientation(R, orientation);
            // Log.d(TAG, "azimuth (rad): " + azimuth);
            azimuth = (float) Math.toDegrees(orientation[0]); // orientation
            azimuth = (azimuth + 360) % 360;

            azimuth -= bearing(yourlatitude, yourlongitude, latWhereToPoint, lngWhereToPoint);
            // Log.d(TAG, "azimuth (deg): " + azimuth);
            adjustArrow();
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文