How Qibla Direction Is Calculated
Two questions look like one but are entirely different: which way is the Kaaba from where I am? and where is my phone's compass pointing right now? The first has a deterministic geometric answer. The second is a sensor reading affected by its surroundings. This page explains the first precisely, and why neither replaces the other.
The Qibla is a direction on a sphere, not a line on a flat map
What confuses most people is picturing a wall map and joining their city to Makkah with a ruler. But the Earth isn't flat, and Mercator maps distort direction increasingly with latitude.
The Qibla is the shortest path across the surface of a sphere — a great-circle arc — and the direction you need is the angle at which you set off along that arc.
That's why the results defy intuition. From New York the Qibla is 58°, north-east rather than east, even though Makkah sits to the south-east on a flat map. From Cape Town it's 23°, from Tokyo 293°, and from Jakarta 295° — north-west in the latter two.
The formula used
The initial bearing of the great circle between two points is computed with the standard formula:
y = sin(Δλ) · cos(φ₂)
x = cos(φ₁) · sin(φ₂) − sin(φ₁) · cos(φ₂) · cos(Δλ)
θ = atan2(y, x)
where φ₁ is your latitude, φ₂ the Kaaba's, and Δλ the difference in longitude. The radian result is converted to degrees.
This bearing is referenced to geographic north, because the equation is purely geographic and contains no magnetic term. We'll return to that.
The displayed value is the initial great-circle bearing from your location toward the Kaaba. If someone physically travelled along that great-circle route, the heading would gradually change along the journey. While you stay where you are, the value is fixed.
The Earth model used
TimesPrayers uses a spherical Earth model for this calculation. The bearing and distance are therefore based on great-circle geometry on a sphere rather than a more complex ellipsoidal geodesic model.
The Kaaba's coordinates
The coordinates used by TimesPrayers are 21.4225°N, 39.8262°E. It's a single fixed point, so the result changes only when your own position changes.
Distance
Distance is computed with the Haversine formula on a sphere of radius 6,371 km and displayed rounded to the nearest kilometre. It's a surface distance along the arc, not a straight line through the Earth.
| City | Bearing | Direction | Distance |
|---|---|---|---|
| Cairo | 136.14° | south-east | 1,287 km |
| Riyadh | 243.80° | south-west | 790 km |
| Sanaa | 326.28° | north-west | 815 km |
| Ankara | 160.17° | south | 2,162 km |
| Moscow | 176.36° | south | 3,822 km |
| London | 118.99° | south-east | 4,794 km |
| Cape Town | 23.35° | north-east | 6,558 km |
| Jakarta | 295.15° | north-west | 7,920 km |
| Tokyo | 293.00° | north-west | 9,472 km |
| New York | 58.48° | north-east | 10,306 km |
| São Paulo | 68.94° | east | 10,602 km |
| Sydney | 277.50° | west | 13,236 km |
Note Riyadh and Sanaa: both are close to Makkah, yet the Qibla from Riyadh is south-west and from Sanaa north-west. Bearing depends on relative position, not on distance.
Why the value is sometimes negative before normalisation
atan2 returns a value between −180° and 180°. Any bearing beyond 180° therefore comes out negative, and is normalised by adding 360 and taking the remainder modulo 360:
| City | Raw result | After normalisation |
|---|---|---|
| Sanaa | −33.72° | 326.28° |
| Jakarta | −64.85° | 295.15° |
| Tokyo | −67.00° | 293.00° |
| Sydney | −82.50° | 277.50° |
| Riyadh | −116.20° | 243.80° |
This site shows the bearing in two places: a whole number on the visible badge, and two decimal places in the details grid. Both come from the same calculation; only the display differs.
Your device's compass is not this calculation
This is the crux. The number above is deterministic: give it the same coordinates and it returns the same result every time. The needle moving on your screen comes from the device's sensors, and behaves quite differently.
Two different paths for two platforms
On iOS the site uses the heading the system provides directly. On Android the heading is derived from the device's rotation matrix with compensation for tilt and screen rotation, so the reading doesn't drift when you hold the phone at an angle or rotate the screen.
Absolute versus relative
Browser specifications distinguish two kinds of orientation reading: absolute, which references the Earth's frame, and relative, whose reference frame is arbitrary — meaning its "zero" may not be north at all.
So this site prefers the absolute reading, and once it has seen one it ignores subsequent relative readings, so an arbitrary zero can't corrupt a correct heading. If only a relative reading is available, the site shows a low-compass-accuracy notice. If no reading is available at all, it hides the live needle and shows the computed bearing as a fixed number.
Geographic north versus magnetic north
The mathematical Qibla bearing is referenced to geographic north, while the live compass heading comes from device sensors and may be magnetically referenced. The angle between the two is called magnetic declination; it varies by location and changes over time — which is why global models of it are revised every five years.
TimesPrayers does not currently apply a separate magnetic-declination correction. So the needle may not land exactly on the computed number.
Accuracy is not the number of decimal places
A value displayed to a decimal place does not mean the real compass direction is accurate to that degree; sensor accuracy depends on the device, its surroundings, and the magnetic field. Magnetometers are sensitive to what's around them: metal objects, speakers, magnetic phone cases, cars, and reinforcing steel in buildings. Moving the phone in a figure-of-eight helps the system recalibrate.
The practical takeaway: the computed number is the stable reference; the needle is an approximate tool for acting on it.
On small deviation
Many people ask what happens if they're slightly off the computed direction. That is a matter of jurisprudence, treated by scholars with detail that varies according to a person's circumstances and their ability to determine the direction. We present the calculation and its limits, and issue no ruling on it; that is referred to qualified scholars or the authority a user follows.
Sources
The bearings and distances on this page were computed with the same formula this site uses, from each city's coordinates.
- Initial bearing, Haversine and the 6,371 km radius on a spherical model: movable-type.co.uk
- Earth reference frame for the absolute orientation sensor: w3.org/TR/orientation-sensor
- Absolute versus relative readings and display-precision limits: w3.org/TR/orientation-event
webkitCompassHeadingand its reference: Apple Developer documentation- Magnetic declination and how it changes: World Magnetic Model — ncei.noaa.gov