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.
Asked
Active
Viewed 9,522 times
7
-
1You need to be a lot more specific. – Jun 04 '12 at 19:56
-
Are you trying to program one? What are you having trouble with? What have you tried? – CaseyB Jun 04 '12 at 20:00
-
USE this https://github.com/imranbaigshortcut/android-qibla-finder – Imran Baig Jul 07 '22 at 07:18
1 Answers
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.
-
I am getting erro: `Cannot invoke toDeg() on the primitive type double` – MD SHAHIDUL ISLAM May 26 '14 at 11:47
-
2Solved it `float brng = (float) Math.toDegrees(Math.atan2(y, x));` – MD SHAHIDUL ISLAM May 26 '14 at 12:03
-
Use this customizable library https://github.com/imranbaigshortcut/android-qibla-finder – Imran Baig Jul 07 '22 at 07:17