简体   繁体   中英

Get angle between two point(LatLng) with vertical axis (north axis)

At the moment , I have two point(LatLng) A and B on the google map. I present my problem as image follows: 在此处输入图片说明

When rotate device in vertical axis,I want to get angle @ between point A and B 在此处输入图片说明

I want to get angle between AB and north-axis (y-axis) How must I do. I created a function to get angle but it return null :(

    public double getAngleTwoPoint(LatLng point1,LatLng point2){
    double lat1=point1.latitude;
    double lat2=point2.latitude;
    double long1=point1.longitude;
    double long2=point2.longitude;
    double deltaLong=long2-long1;
    double angle = Math.atan2(Math.sin(deltaLong)*Math.cos(lat2), Math.cos(lat1)*Math.sin(lat2)-Math.sin(lat1)*Math.cos(lat2)*Math.cos(deltaLong));     
    return Math.toDegrees(angle);
}

you can use this formula to get angle between to lat longs have a look.

θ = atan2(sin(Δlong)*cos(lat2), cos(lat1)*sin(lat2) − sin(lat1)*cos(lat2)*cos(Δlong))

Note that the angle(θ) should be converted to radians before using this formula and Δlong = long2 - long1.

atan2 is a common function found in almost all programming languages (mostly in the Math package). Usually there is also functions for conversion between degrees and radians(also in the Math package).

Remember that atan2 returns values in the range of -π ... +π, to convert the result to a compass bearing, you need to multiply θ by 180/π then use (θ+360) % 360, where % is modulus division operation returning the remainder of the division.

The following link is a good resource for formulas involving latitudes and longitudes. They also provide Javascript implementation of their formulas. In fact the answer is based on the information from this page:

http://www.yourhomenow.com/house/haversine.html

If you have triangle

CB
A

then the angle between AB and y-axis is equal to CAB angle. tg(CAB) = BC/AC, you need the angle, so actually what you are loking for is arctg(BC/AC).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM