在谷歌映射表GeoPoints在机器人之间的方向 [英] Direction between two GeoPoints in Google map in android

查看:95
本文介绍了在谷歌映射表GeoPoints在机器人之间的方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个Android应用程序中我在谷歌map.One的GeoPoint 2 GeoPoints被修复等的GeoPoint是我现在的位置。 在我当前的位置,我把箭头点修复的GeoPoint的方向,因为我现在的位置变化,使箭头将改变其direction.It工作正常,但在大多数情况下,它显示错误的方向。

I am developing an android application in I have two GeoPoints in Google map.One GeoPoint is fix and other GeoPoint is my current location. On my current location I am placing an arrow point in direction of fix GeoPoint,as my current location changes so the arrow will change its direction.It works fine but in most cases it showing wrong direction.

下面是我的code。

            Paint paint = new Paint();
        GeoPoint gp1;
        GeoPoint gp2;
        Point point1,point2;
        Projection projection = mapView.getProjection();
        projection.toPixels(gp1, point1);
        projection.toPixels(gp2, point2);
        Bitmap bmp = BitmapFactory.decodeResource(getResources(),R.drawable.arrow);
        double dlon = gp2.getLongitudeE6() - gp1.getLongitudeE6();
        double dlat = gp2.getLatitudeE6() - gp1.getLatitudeE6();
        double a = (Math.sin(dlat / 2) * Math.sin(dlat / 2)) + Math.cos(gp1.getLatitudeE6()) * Math.cos(gp2.getLatitudeE6()) * (Math.sin(dlon / 2) * Math.sin(dlon / 2));
        double angle = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
        Matrix matrix = new Matrix();
        matrix.postTranslate(-25, -25);
        matrix.postRotate(convertToRadians(angle));
        matrix.postTranslate(point1.x, point1.y);       
        paint.setAntiAlias(true);
        paint.setFilterBitmap(true);
        canvas.drawBitmap(bmp, matrix, paint);

等待您的帮助,在此先感谢。

Waiting for your help and thanks in advance.

阿尔塔夫

推荐答案

你从哪里得到这个公式?这似乎令人难以置信的复杂,一个简单的角度计算。通常我只是用ATAN2与Y,X:

Where did you get that formula? It seems incredibly complex for a simple angle calculation. Normally I just use atan2 with y, x :

double angle = Math.atan2(dlat, dlon); // in radians

这是假设你的箭头沿水平轴(正东)默认指向。您可能需要通过任何方向的箭头可绘制指向调整它。例如,如果它的指向恒定90度关闭,只是它旋转一个附加0.5 * PI拉德至对齐。

This is assuming your arrow is pointing along the horizontal axis(due east) by default. You might have to tweak it by whatever direction your arrow drawable is pointing. For example, if it's pointing a constant 90 degrees off, just rotate it an additional 0.5*PI rads to align it.

这篇关于在谷歌映射表GeoPoints在机器人之间的方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆