Angle between 2 GPS Coordinates

瘦欲@ 提交于 2019-12-03 07:50:55

问题


I'm working in another iPhone App that uses AR, and I'm creating my own framework, but I'm having trouble trying to get the angle of a second coordinate relative to the current device position, anyone know a good resource that could help me with this?

Thanks in advance!


回答1:


If the two points are close enough together, and well away from the poles, you can use some simple trig:

float dy = lat2 - lat1;
float dx = cosf(M_PI/180*lat1)*(long2 - long1);
float angle = atan2f(dy, dx);

EDIT: I forgot to mention that latN and longN — and therefore dx and dy — can be in degrees or radians, so long as you don't mix units. angle, however, will always come back in radians. Of course, you can get it back to degrees if you multiply by 180/M_PI.




回答2:


Here is the android version of this code

import com.google.android.maps.GeoPoint;
    public double calculateAngle(GeoPoint startPoint, GeoPoint endPoint) {
        double lat1 = startPoint.getLatitudeE6() / 1E6;
        double lat2 = endPoint.getLatitudeE6() / 1E6;
        double long2 = startPoint.getLongitudeE6() / 1E6;
        double long1 = endPoint.getLongitudeE6() / 1E6;
        double dy = lat2 - lat1;
        double dx = Math.cos(Math.PI / 180 * lat1) * (long2 - long1);
        double angle = Math.atan2(dy, dx);
        return angle;
    }


来源:https://stackoverflow.com/questions/3809179/angle-between-2-gps-coordinates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!