Qibla compass in Android

Parag Chauhan picture Parag Chauhan · Jun 4, 2012 · Viewed 8.7k times · Source

I have try much but can not make Qibla compass for my application. I can't understand what I do. I need Qibla compass that works perfectly.

Answer

Simon picture Simon · Jun 4, 2012

You know the location of Mecca and you know the users current location (if you have GPS or some other location provider). The bearing is given by this formula, latitudes and longitudes should be in radians.

float lonDelta = (lon2 - lon1);
float y = Math.sin(lonDelta) * Math.cos(lat2);
float x = Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1) * Math.cos(lat2) * Math.cos(lonDelta);
float brng = Math.atan2(y, x).toDeg();

brng is the direction in degrees.

You could also explore the Location.bearingTo() method, see here.