سؤال

At the moment , I have two point(LatLng) A and B on the google map. I present my problem as image follows: enter image description here

When rotate device in vertical axis,I want to get angle @ between point A and B enter image description here

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).

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top