What is being calculated

The qibla is the initial bearing of the great-circle path from where you are standing to the Kaaba, expressed in degrees clockwise from true north. Two words in that sentence do the work. Great-circle means the shortest route across the curved surface of the Earth, not the straight line you would draw on a flat map. Initial means the heading at the moment you set off: along a great-circle path the compass heading changes continuously, so a single number can only describe the start of it — which is exactly what you need when you are standing still and facing a direction.

The formula

Given your latitude and longitude (φ₁, λ₁) and the Kaaba’s (φ₂, λ₂), with Δλ = λ₂ − λ₁:

θ = atan2( sin Δλ , cos φ₁ · tan φ₂ − sin φ₁ · cos Δλ )

The result is then normalised into the range 0°–360°. This is the standard spherical-trigonometry solution for the angle at one vertex of a spherical triangle whose third vertex is the north pole; the form above is the one given in Todhunter’s Spherical Trigonometry (p. 50), which is the derivation adhan-js cites and the one this site uses unchanged.

Note what is not in the formula: no time, no date, no magnetic field, no altitude. The qibla from a fixed point never changes. Anything in an app that makes the number drift from one day to the next is the compass sensor, not the qibla.

The constants

The Kaaba is taken as 21.4225241° N, 39.8261818° E. Different tools carry slightly different decimals here, and it is worth knowing why that is harmless: the Kaaba is a building of roughly 11 × 13 metres, so there is no single “correct” point. Picking a different corner moves the bearing by well under a thousandth of a degree from anywhere further away than the outskirts of Mecca — orders of magnitude below what a phone compass can resolve.

Worked example: London

London is 51.5074° N, −0.1278° E. Substituting, with every intermediate value rounded to six decimals:

Δλ39.9540°
sin Δλ0.642172
cos φ₁ · tan φ₂0.244204
sin φ₁ · cos Δλ0.599978
denominator−0.355775
θ118.987°

So from London the qibla is 119°, east-south-east. The negative denominator is the part that catches people out: it is why the calculation needs atan2 rather than a plain arctangent, which would put the answer in the wrong quadrant and give a bearing roughly 180° away.

How far the alternatives drift

Two other models get used for this, one defensible and one not. The WGS-84 geodesic treats the Earth as the oblate ellipsoid it actually is and solves the bearing with Vincenty’s method. The rhumb line is the constant-compass-heading path — the direction you get by drawing a straight line to Mecca on a Mercator map, which is what most paper-map and naïve web attempts produce. The figures below are bearings in degrees from true north, computed for nine cities:

Qibla bearing by model, degrees from true north. Sorted by how badly the flat-map answer misses.
CityGreat circleWGS-84 geodesicDifferenceFlat mapFlat-map error
Toronto54.58°54.49°0.09°102.61°48.0°
New York58.48°58.40°0.09°101.28°42.8°
Sydney277.50°277.32°0.18°297.50°20.0°
London118.99°118.87°0.12°133.83°14.8°
Kuala Lumpur292.54°292.44°0.10°286.90°5.6°
Lagos63.33°63.46°0.13°67.09°3.8°
Istanbul151.62°151.51°0.11°154.84°3.2°
Cape Town23.35°23.47°0.11°20.24°3.1°
Jakarta295.15°295.02°0.13°292.79°2.4°

The two columns tell opposite stories. Sphere versus ellipsoid is a difference of 0.09°–0.18° — real, but far below anything a person standing on a prayer mat could act on, and well below the noise of the magnetometer in a phone. Choosing the simpler spherical formula costs nothing that matters.

The flat map is a different matter. In Toronto it is wrong by 48.0° and in New York by 42.8° — the difference between facing north-east and facing east-south-east. This is the single largest source of genuinely wrong qibla directions, and it is worst exactly where the great-circle path bends most: high latitudes, far from Mecca.

Where the error actually comes from

Once the formula is right, the arithmetic stops being the weak link. A double-precision implementation of the equation above is accurate to far more decimal places than anyone needs. What is left is all on the measurement side:

  • Magnetic declination. A phone’s magnetometer reads magnetic north, which differs from true north by an amount that depends on where and when you are — currently past 15° in parts of North America and the far south. A bearing is meaningless until it is stated against one north or the other. Apps that correct for it use a field model such as the WMM; a plain compass app usually does not.
  • Sensor calibration. Magnetometers drift, and nearby metal, speakers, magnets and phone cases deflect them. This is the most common cause of a needle that is confidently wrong by tens of degrees.
  • Position accuracy. The least important of the three. Being a kilometre off in your own position changes the bearing by a hundredth of a degree or so from typical distances; city-level coordinates are entirely sufficient.

The practical consequence is that the number is worth more than the needle. If two tools disagree, compare the bearings in degrees, not the arrows. Ours is shown on the qibla finder, computed in your browser from coordinates that are never sent anywhere.

Reproducing these figures

The great-circle column is Qibla(new Coordinates(lat, lng)) from adhan-js — the same call this site’s compass makes. The geodesic column is a Vincenty inverse solution on the WGS-84 ellipsoid (a = 6378137 m, f = 1/298.257223563), taking the initial bearing. The flat-map column is the rhumb-line bearing, atan2(Δλ, Δψ), where Δψ is the difference of the inverse Gudermannian (stretched Mercator) latitudes. All three take the same Kaaba constants given above.

If you get a different number for any city in that table, we would rather hear about it than not — the contact page reaches us, and a correction is worth more to this site than being right was.

Common questions

What is the formula for the qibla direction?
The initial great-circle bearing from your position to the Kaaba: θ = atan2( sin(Δλ), cos(φ₁)·tan(φ₂) − sin(φ₁)·cos(Δλ) ), where φ₁ and λ₁ are your latitude and longitude, φ₂ and λ₂ the Kaaba's, and Δλ = λ₂ − λ₁. The result is normalised to 0–360° clockwise from true north.
What coordinates are used for the Kaaba?
21.4225241° N, 39.8261818° E. Published qibla tools differ in the last decimals because the Kaaba is a building roughly 11 by 13 metres, not a point; choosing a different corner of it changes the bearing by far less than a thousandth of a degree from any distance beyond a few kilometres.
Does using a sphere instead of the WGS-84 ellipsoid make the qibla wrong?
Not measurably in practice. Across the nine cities measured on this page, the spherical formula and a full WGS-84 geodesic (Vincenty) bearing differ by between 0.086° and 0.181° — smaller than the error of any phone magnetometer, and far smaller than the error from holding the phone near metal.
Why is a straight line to Mecca on a flat map wrong?
Because a Mercator map preserves constant compass bearings, not shortest paths. That constant bearing (a rhumb line) differs from the true qibla by 48.0° in Toronto and 42.8° in New York — enough to face an entirely different direction.

← Find your qiblaPrayer times →