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?
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?
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;
}
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.