Quick answer: The Qibla direction is the great-circle bearing from your location to the Ka'bah in Makkah (about 21.4225° N, 39.8262° E). The formula is: q = atan2(sin Δλ, cos φ · tan φk − sin φ · cos Δλ), where φ is your latitude, φk the Ka'bah's latitude and Δλ the longitude difference. The result is degrees clockwise from true north. For example, from Riyadh the Qibla is about 244°, from London about 119°.

Facing the Qibla is a condition of salah. Allah says: "So turn your face toward al-Masjid al-Haram. And wherever you are, turn your faces toward it." (Al-Baqarah 2:144). Today most Muslims find the Qibla with a phone app. This guide explains the mathematics behind those apps, gives tested code, and lists the common mistakes developers make.

Why not just draw a straight line on a map?

The Earth is a sphere, and flat maps distort direction. The correct Qibla follows the shortest path on the globe (a great circle). That is why the Qibla from New York points north-east (about 58°), not south-east as a flat map might suggest.

The formula

  • φ = your latitude, λ = your longitude
  • φk = 21.4225°, λk = 39.8262° (the Ka'bah)
  • Δλ = λk − λ
q = atan2( sin(Δλ),  cos(φ)·tan(φk) − sin(φ)·cos(Δλ) )
bearing = (q in degrees + 360) mod 360

JavaScript implementation

const KAABA = { lat: 21.4225, lng: 39.8262 };
const rad = d => d * Math.PI / 180;

function qiblaBearing(lat, lng) {
  const phi = rad(lat);
  const phiK = rad(KAABA.lat);
  const dLng = rad(KAABA.lng - lng);
  const q = Math.atan2(
    Math.sin(dLng),
    Math.cos(phi) * Math.tan(phiK) - Math.sin(phi) * Math.cos(dLng)
  );
  return (q * 180 / Math.PI + 360) % 360; // degrees from TRUE north
}

function distanceToKaabaKm(lat, lng) {
  const dLat = rad(KAABA.lat - lat), dLng = rad(KAABA.lng - lng);
  const a = Math.sin(dLat / 2) ** 2 +
            Math.cos(rad(lat)) * Math.cos(rad(KAABA.lat)) * Math.sin(dLng / 2) ** 2;
  return 2 * 6371 * Math.asin(Math.sqrt(a));
}

console.log(qiblaBearing(24.7136, 46.6753).toFixed(1)); // Riyadh ≈ 243.8

Tested results

City

Qibla (from true north)

Distance to Makkah

Riyadh

243.8°

≈ 790 km

Karachi

267.7°

≈ 2,800 km

London

119.0°

≈ 4,794 km

Jakarta

295.2°

≈ 7,920 km

New York

58.5°

≈ 10,306 km

Turning the bearing into a compass arrow

Phones give you a heading (which way the top of the phone points). The arrow angle on screen is:

arrowRotation = (qiblaBearing - deviceHeading + 360) % 360

Browser example

window.addEventListener('deviceorientationabsolute', e => {
  const heading = 360 - e.alpha;          // alpha: 0 = north on most devices
  const rotate = (qibla - heading + 360) % 360;
  arrow.style.transform = 'rotate(' + rotate + 'deg)';
});

On iOS Safari, use event.webkitCompassHeading and request permission with DeviceOrientationEvent.requestPermission() after a user tap.

Common mistakes

  1. Magnetic vs true north – compasses point to magnetic north. The difference (magnetic declination) can be several degrees. Use the platform's true heading when available (Android GeomagneticField, iOS trueHeading).
  2. Degrees vs radians – the most common bug. All trig functions use radians.
  3. Wrong Ka'bah coordinates – use at least four decimal places.
  4. Uncalibrated sensors – ask users to move the phone in a figure-8 and keep it away from metal and magnets.
  5. Flat-map logic – never compute Qibla with simple x/y slopes.

Fiqh notes

  • Someone who can see the Ka'bah must face it exactly; someone far away must face its direction. The Prophet ﷺ said: "What is between the east and the west is a qiblah" (Tirmidhi 342, for the people of Madinah), which scholars use to show that small deviations for those far away are forgiven.
  • If a person makes ijtihad, prays, and later learns the direction was slightly wrong, the prayer is valid according to the majority of scholars.
  • Do your best with reliable tools – that is what is required.

Summary

  • Qibla = great-circle bearing to 21.4225° N, 39.8262° E.
  • Use atan2 formula; return degrees from true north.
  • Adjust for device heading and magnetic declination.
  • Test against known cities before releasing your app.