7

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Parag Chauhan
  • 35,760
  • 13
  • 86
  • 95

1 Answers1

10

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Simon
  • 14,407
  • 8
  • 46
  • 61