1

I took the code of compass from this link.

http://www.androidcode.ninja/android-compass-code-example/

How to set mecca location on my compass??

What do i need to do to point my compass to Mecca?

  • 2
    http://math.stackexchange.com/questions/796243/how-to-determine-the-direction-of-one-point-from-another-given-their-coordinate – Mohammed Atif Mar 31 '17 at 14:36

2 Answers2

1

You need to determine your location relatively to Mecca, so you will need Location permission and implement Location Updates.

You can implement that using: Receveing location updates

With this information you can determine where Mecca would be on your compass and set it accordingly. To determine the angle, use this code:

private double angleFromCoordinate(double lat1, double long1, double lat2,
    double long2) {

double dLon = (long2 - long1);

double y = Math.sin(dLon) * Math.cos(lat2);
double x = Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1)
        * Math.cos(lat2) * Math.cos(dLon);

double brng = Math.atan2(y, x);

brng = Math.toDegrees(brng);
brng = (brng + 360) % 360;
brng = 360 - brng; // count degrees counter-clockwise - remove to make clockwise

return brng;
}

from: Calculate angle between two Latitude/Longitude points

Community
  • 1
  • 1
Stefan
  • 2,098
  • 2
  • 18
  • 29
0

Well for very few words that's a long question. The answer is to extend the OnSensorChanged code from the example to calculate the heading to Mecca from the current location and adjust the heading based on the current direction the sensor is facing in. You'll need the current location co-ordinates from GPS and the fixed co-ordintates for Mecca, Calculate the direction relative to North (the way the compass is designed to point) and then adjust by the current heading.

Charemer
  • 196
  • 1
  • 9