Question

I want to calculate the distance between 2 lat & long. I am able to calculate the distance as below

CLLocation *currentLoc = [[CLLocation alloc] initWithLatitude:-24.4132995 longitude:121.0790024];
CLLocation *restaurnatLoc = [[CLLocation alloc] initWithLatitude:-32.8310013 longitude:150.1390075];
CLLocationDistance meters = [restaurnatLoc distanceFromLocation:currentLoc];
NSLog(@"Distance between 2 geo cordinates: %.2f Meters",meters);

Now I want to get the direction from currentLocation to restaurnatLoc. For this I have below code

double DegreesToRadians(double degrees) {return degrees * M_PI / 180;};
double RadiansToDegrees(double radians) {return radians * 180/M_PI;};

-(double) bearingToLocationFromCoordinate:(CLLocation*)fromLoc toCoordinate:(CLLocation*)toLoc
{
    double lat1 = DegreesToRadians(fromLoc.coordinate.latitude);
    double lon1 = DegreesToRadians(fromLoc.coordinate.longitude);
    
    double lat2 = DegreesToRadians(toLoc.coordinate.latitude);
    double lon2 = DegreesToRadians(toLoc.coordinate.longitude);
    
    double dLon = lon2 - lon1;
    
    double y = sin(dLon) * cos(lat2);
    double x = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(dLon);
    double radiansBearing = atan2(y, x);
    
    return RadiansToDegrees(radiansBearing);
}

It returns bearing = 114.975752 Now how can I decide whether restaurant is in North,South,West,East,NW,NE,SW,SE from my current location ?

I get 1 solution from this link Direction based off of 2 Lat,Long points But if I consider this solution , then I have doubt on bearing 114 from my location(red circle) to restaurant (green circle) as shown below. Correct me if I am wrong.

enter image description here

As current location is "Western Australia" & restaurant location is "Sydney" as shown in Google Maps.

Can any body tell me whats going wrong here ? Thanks.

///////////////////////////// Update /////////////////////////////

My compass diagram is wrong. Here is the correct diagram all thanks to AlexWien

enter image description here

Now I am getting the correct output

Was it helpful?

Solution

your compass rose is totally wrong. have you ever looked at a compass? open the iphone compass app and look where 90Degrees is located. It is east, not west like in your graphic.

geographical direction is measured clockwise!

so 114 deg is east, which matches you expectation

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top