Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

iphone - How to get angle between two POI?

How do I calculate the angle in degrees between the coordinates of two POIs (points of interest) on an iPhone map application?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I'm guessing you try to calculate the degrees between the coordinates of two points of interest (POI).

Calculating the arc of a great circle:

+(float) greatCircleFrom:(CLLocation*)first 
                      to:(CLLocation*)second {

    int radius = 6371; // 6371km is the radius of the earth
    float dLat = second.coordinate.latitude-first.coordinate.latitude;
    float dLon = second.coordinate.longitude-first.coordinate.longitude;
    float a = pow(sin(dLat/2),2) + cos(first.coordinate.latitude)*cos(second.coordinate.latitude) * pow(sin(dLon/2),2);
    float c = 2 * atan2(sqrt(a),sqrt(1-a));
    float d = radius * c;

    return d;
}

Another option is to pretend you are on cartesian coordinates (faster but not without error on long distances):

+(float)angleFromCoordinate:(CLLocationCoordinate2D)first 
               toCoordinate:(CLLocationCoordinate2D)second {

    float deltaLongitude = second.longitude - first.longitude;
    float deltaLatitude = second.latitude - first.latitude;
    float angle = (M_PI * .5f) - atan(deltaLatitude / deltaLongitude);

    if (deltaLongitude > 0)      return angle;
    else if (deltaLongitude < 0) return angle + M_PI;
    else if (deltaLatitude < 0)  return M_PI;

    return 0.0f;
}

If you want the result in degrees instead radians, you have to apply the following conversion:

#define RADIANS_TO_DEGREES(radians) ((radians) * 180.0 / M_PI)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...