Methodology · Qibla

How We Calculate the Qibla Direction

Every step from your position to the needle on your screen: the reference point, the formula, the distance, true north, the phone sensors, the tests and the limits.

Version 1.0Updated Reviewed by [Reviewer name], [role]About 12 min read

Interactive tool

Calculate the Qibla for any place, step by step

Pick a city or type any coordinates. The globe, the compass and every step of the formula update together. Lahore is the worked example; each step is explained in the sections below.

Qibla Explorer

Tap the globe. Watch the Qibla move.

LahoreMakkah
Tap anywhere on the globe to stand there
Qibla from Lahore260.4°9.6° south of west3,598 km to the Kaaba
The bearing at your place
NMAGθ

True north · magnetic north 2.2° E · Qibla θ

Why not a straight line on a map?
True Qibla (great circle)260.4°
Flat-map line (rhumb line)251.9°

A straight line on a flat map would point 8.5° away from the true Qibla. The Earth is round, so the shortest path bends: we use the great circle.

Show the maths for Lahore
Step by step calculation
StepFormulaValue
Your position φ₁, λ₁GPS or city centre31.5204°, 74.3587°
Kaaba φ₂, λ₂fixed reference, WGS 8421.4225°, 39.8262°
Δλλ₂ − λ₁−34.5325°
ysin Δλ · cos φ₂−0.52771
xcos φ₁ · sin φ₂ − sin φ₁ · cos φ₂ · cos Δλ−0.08958
θatan2(y, x)−99.634°
Qibla(θ + 360°) mod 360°260.366°
Shownrounded to 0.1°, in words260.4° · 9.6° south of west
asin²(Δφ/2) + cos φ₁ · cos φ₂ · sin²(Δλ/2)0.077656
Distance d2R · asin(√a), R = 6,371.0088 km3598.4 km
Magnetic compassQibla − declination258.2°

At a glance

TargetCentre of the Kaaba, 21.4225° N, 39.8262° E (WGS 84)
Your positionSaved city, browser location after you allow it, or a searched city (city centre)
BearingInitial great-circle bearing on a sphere, clockwise from true north
DistanceHaversine, mean Earth radius 6,371.0088 km
North on phonesMagnetic heading + declination from the World Magnetic Model (WMM2025)
Shown asBearing to 0.1°, distance to 1 km, direction in words
RunsIn your browser. Your location is not sent to our server.

1 · The reference point: the Kaaba

The Qibla is the direction of the Kaaba in Al-Masjid al-Haram, Makkah (Quran 2:144). We use the centre of the building: 21.4225° N, 39.8262° E on the WGS 84 datum, the same datum GPS uses.

Four decimal places are about 11 m on the ground. Moving the target point by the full size of the Kaaba changes the bearing from Lahore by far less than 0.01°, so the choice of point inside the building has no visible effect anywhere outside Makkah.

2 · Your position

The page needs one pair of coordinates. It takes them in this order and never guesses from your IP address:

  1. Saved city. If you saved a city on MiqatLive, we use its centre from our city database (GeoNames).
  2. Use my location. Only after you tap the button and your browser asks for permission. We use the coordinates and their accuracy radius from the browser’s Geolocation API.
  3. City search. The centre of the city you pick.

The page HTML is the same for every visitor and for search engines. Nothing about your place is rendered on the server.

3 · The bearing formula

The Qibla is the direction in which the shortest path over the Earth, a great circle, leaves your position toward the Kaaba. This is the initial bearing (forward azimuth):

θ = atan2( sin Δλ · cos φ₂ , cos φ₁ · sin φ₂ − sin φ₁ · cos φ₂ · cos Δλ ) Qibla = (θ + 360°) mod 360°
  • φ₁, λ₁: your latitude and longitude. φ₂, λ₂: the Kaaba.
  • Δλ = λ₂ − λ₁. Angles in radians inside the functions.
  • atan2 keeps the right quadrant, so the result works anywhere on Earth, east or west, north or south of Makkah.
  • The result is measured clockwise from true north: 0° north, 90° east, 180° south, 270° west. Lahore: θ = −99.634°, so the Qibla is 260.366°, shown as 260.4°.

4 · Distance to the Kaaba

The distance is the length of the same great circle, from the haversine formula:

a = sin²(Δφ/2) + cos φ₁ · cos φ₂ · sin²(Δλ/2) d = 2R · asin(√a) R = 6,371.0088 km

Lahore: a = 0.077656, central angle 0.564814 rad, d = 3,598.4 km, shown as 3,598 km. R is the mean radius of the Earth defined by the IUGG.

5 · Sphere or ellipsoid?

The Earth is slightly flattened at the poles. A geodesic on the WGS 84 ellipsoid (Karney’s method) is more exact than a great circle on a sphere. We compared both for ten cities:

CitySphereEllipsoidDifferenceDistance (sphere · ellipsoid)
Lahore260.37°260.46°0.09°3,598 · 3,603 km
Karachi267.74°267.79°0.04°2,800 · 2,804 km
Islamabad255.91°256.02°0.11°3,528 · 3,531 km
Peshawar253.99°254.10°0.11°3,401 · 3,405 km
Quetta256.66°256.76°0.10°2,878 · 2,881 km
Dubai258.23°258.31°0.08°1,631 · 1,633 km
Istanbul151.62°151.51°0.11°2,405 · 2,400 km
London118.99°118.87°0.12°4,794 · 4,795 km
Jakarta295.15°295.02°0.13°7,920 · 7,922 km
New York58.48°58.40°0.09°10,306 · 10,324 km

The difference stays near 0.1°, which is about 2 cm across a 10 m prayer hall and far below what a phone compass can resolve. We use the sphere so that every number can be checked by hand with the formula above. Decision: MiqatLive uses the sphere. You check the Qibla with a phone, and a phone compass is off by several degrees, far more than the 0.1° difference.

6 · True north and magnetic north

The Qibla bearing is measured from true north, the direction of the geographic North Pole. A magnetic compass points to magnetic north. The angle between them is the magnetic declination; it depends on the place and changes slowly every year.

true heading = magnetic heading + declination (east declination is positive)
  • We take the declination for your coordinates and today’s date from the World Magnetic Model WMM2025, published by NOAA NCEI and the British Geological Survey, valid 2025–2030.
  • Where a phone already reports a heading relative to true north, we use it as it is and do not add the declination twice.
  • If you use a separate hand compass, set the local declination on it or add it yourself.

7 · The live compass on phones

On a phone the dial turns with you so the Kaaba mark always points at the Qibla. The steps behind it:

1 · PermissionYou tap “Turn on live compass”. iPhone asks for motion and orientation access.
2 · HeadingThe device orientation sensor reports where the top of the phone points.
3 · True northDeclination added, screen rotation corrected.
4 · SmoothingReadings are averaged on the circle, so 359° and 1° average to 0°, not 180°.
5 · DisplayDial turns by −heading; the Kaaba mark sits at the Qibla bearing.
  • Turn hint. The page shows “turn 12° right” from the difference between the Qibla bearing and your heading.
  • Aligned. Within ±3° the gold ring lights up and the phone vibrates once, where the browser supports it.
  • Calibration. When the sensor reports low accuracy, the page asks you to move the phone in a figure 8.
  • Hold it flat. The heading is most reliable with the screen facing up; tilted phones are corrected but less stable.
  • Interference. Steel, cars, speakers, magnets and magnetic phone cases bend the reading by several degrees. No software can remove that.

8 · Desktop and the map

Laptops and desktops have no compass sensor. There the dial shows the bearing with north at the top, and a map draws the great circle from your place to Makkah.

The map is a flat (equirectangular) world map, so the shortest path appears as a curve. The line is drawn from 49 points calculated along the great circle.

9 · Rounding and direction in words

  • Bearing to one decimal (260.4°). Distance to the nearest kilometre (3,598 km).
  • Direction in words is measured from the nearest cardinal point: 260.4° is 270° − 9.6°, so “9.6° south of west”, written as “about 10° south of due west”.
  • All numbers use Western digits on every language version.

10 · Sun, moon and Rasd al-Qibla

Rasd al-Qibla is the moment the sun is directly over the Kaaba: the sun’s declination equals the Kaaba’s latitude (21.4225° N) while the sun crosses Makkah’s meridian. This happens twice a year, around 27–28 May (about 12:17 PM Makkah time) and 15–16 July (about 12:26 PM). At that minute, the shadow of any upright object points directly away from the Qibla, wherever the sun is up.

Around 28 November and 13 January the sun is over the point opposite the Kaaba. Then shadows point toward the Qibla; this is useful in the Americas, where it is daytime.

Our prayer engine computes the exact minute each year from the sun’s position (Meeus, Astronomical Algorithms) and converts it to your local time.

Sun at Dhuhr. At local solar noon the sun is due south (northern hemisphere, outside the tropics). Face it and turn right by (Qibla − 180°). In the tropics and the southern hemisphere the sun can be north at noon; the page adjusts the instruction.

11 · Edge cases

  • Inside Al-Masjid al-Haram. Within 1 km of the Kaaba the page says “You are at the Kaaba” instead of a bearing: face the building.
  • The opposite point. At 21.4225° S, 140.1738° W in the South Pacific, every direction is a shortest path. Close to it the bearing changes fast; the page warns about this.
  • Near the poles. The formula still works; the phone compass becomes unreliable, so the page relies on the bearing and the sun.
  • Low location accuracy. If the browser’s accuracy radius is over 5 km, we show it and suggest a city search.

12 · How we test

  • A golden table of cities with bearings and distances checked against an independent geodesic library. A release fails if any bearing differs by more than 0.2°.
  • Unit tests for both sides of the date line, the southern hemisphere, the poles, the Kaaba itself and its opposite point.
  • Field checks on Android and iPhone against the mihrab of known mosques and against Rasd al-Qibla shadows. [Results to be added]

13 · Privacy

  • The calculation runs in your browser. Your coordinates and compass readings are not sent to MiqatLive.
  • Nothing is stored unless you choose “Save city”; then only the city is kept on your device.
  • No location-based redirects; the page is the same for everyone.

14 · Sources

  1. The Quran, Surah Al-Baqarah 2:142–150 (change of the Qibla, 2:144).
  2. Sahih al-Bukhari 40 (sixteen or seventeen months toward Bayt al-Maqdis).
  3. NGA, World Geodetic System 1984 (WGS 84).
  4. NOAA NCEI and British Geological Survey, World Magnetic Model WMM2025.
  5. C. F. F. Karney, “Algorithms for geodesics”, Journal of Geodesy 87 (2013).
  6. J. Meeus, Astronomical Algorithms, 2nd ed. (1998).
  7. W3C, Geolocation API and DeviceOrientation Event specifications.
  8. GeoNames, city coordinates.
  9. Kaaba coordinates: [source to confirm].

15 · Changes to this page

DateChange
Version 1.0 published.