Mecca angle from current location with iPhone compass

I have a question about the Qibla direction, I am creating an iPhone app that will show the north direction and Qibla direction. I show the north direction with CLLocationManager and update it with CLHeading as newHeading.magneticHeading, and I show the Qibla direction with the following code

double A = MECCA_LONGITUDE - lon; double b = 90.0 - lat; double c = 90.0 - MECCA_LATITUDE; NSLog(@"tan -1( sin(%f) / ( sin(%f) * cot(%f) - cos(%f) * cos(%f)))", A, b, c, b, A); double qibAngle = atan(sin(A) /( sin(b) * (1 / tan(c)) - cos(b) * cos(A) )); NSLog(@"qib Angle- %f",qibAngle); qibla.transform = CGAffineTransformMakeRotation(qibAngle * M_PI /180); 

So, here I get the angle, but it does not update the angle when the device is rotated. Can someone help me, I know that I need to do something with the header, but I donโ€™t know what to do?

+4
source share
2 answers

I assume that the code you entered calculates the angle between geographic north and the direction to Mecca for the current location. All you have to do is consider the user header.

For example, suppose a user is located in such a way that Mecca directly belongs to the West, and the user collides directly with the East. Since tan returns +/- 90 degrees, the qibla angle should be -90 degrees. The adjustment should now be obvious: you need to subtract 90 degrees from the qibla angle corresponding to the geographic north (-90) in order to reach (-180) degrees, namely, how much the user needs to turn to meet Mecca.

Simply put, you need to โ€œcancelโ€ the user's rejection, and you do this by subtracting the userโ€™s heading from the qibla corner, which refers to the geographical north.

With the math aside, now you need to observe the course changes and recalculate the qibla angle when changing the title. Finally, make sure you use the trueHeading property.

+1
source

I'm probably going to lose points in this answer because I know absolutely nothing about ios, but I believe atan returns a value in radians and CGAffineTransformMakeRotation also takes an argument in radians , so the qibAngle * M_PI /180 conversion is not required.

You might want to make amends for your post again, as most people donโ€™t know what Qibla is and donโ€™t understand what mathematics and iOS are. I just looked because I heard that calculating the right direction to Mecca is a pretty neat mathematical problem.

+1
source

Source: https://habr.com/ru/post/1402182/


All Articles