How the Qibla direction is calculated

Behind the needle is a short piece of spherical geometry and a tiny magnetic sensor. Here is how both work, in plain language.

What the Qibla is

The Qibla is the direction of the Kaaba in the Sacred Mosque (Masjid al-Haram) in Makkah, which Muslims face during prayer. Al Qibla Finder uses the Kaaba’s coordinates, 21.4225° N, 39.8262° E, as the target for every calculation.

Why the shortest path is curved

The Earth is a sphere, and the shortest route between two points on a sphere follows a great circle, like the routes long-distance flights take. On a flat map that route looks curved, which is why the true Qibla often differs from what a straight line on a map suggests.

From New York, a straight line on a typical world map points east-southeast. The real Qibla from New York is about 58° clockwise from North, toward the northeast, because the shortest path crosses the North Atlantic.

On a flat map the straight line (dashed) looks shortest, but on the globe the true shortest path to the Kaaba curves (gold).

The calculation

With your latitude φ and longitude λ, and the Kaaba’s latitude φK and longitude λK, the direction θ measured clockwise from true North is:

θ = atan2( sin(Δλ) · cos(φK),
           cos(φ) · sin(φK) − sin(φ) · cos(φK) · cos(Δλ) )

where Δλ = λK − λ

The distance to the Kaaba uses the haversine formula with an average Earth radius of 6,371 km. Both run instantly on your device, without contacting any server.

Advertisement

How your phone knows where it is facing

Most phones contain a magnetometer, a small sensor that measures the Earth’s magnetic field. Your phone combines it with its motion sensors to estimate which way the top of the screen points. The browser shares that heading with the page, and the dial rotates so the Kaaba marker stays fixed on the Qibla while you turn.

On iPhone and iPad, Safari asks for permission before sharing motion and orientation data, which is why you see an extra prompt.

Magnetic north and true North

The Qibla angle is measured from true North, the direction of the North Pole. Phone compasses are based on the magnetic field, which can differ from true North by a few degrees. In most of the Middle East, Africa, Europe and Asia the difference is small. In parts of North America it can reach 10° or more, so a quick check against a known landmark is worthwhile.

Where your location comes from

When you allow location access, your browser provides your coordinates from GPS, Wi-Fi or the mobile network. A rough location is enough: being off by several kilometres changes the angle by only a tiny fraction of a degree. If you prefer not to share your location, choose your city instead and the result is just as useful.