Created
June 29, 2019 07:49
-
-
Save ghulamostafa/20cd3422d367634265e395df0286f484 to your computer and use it in GitHub Desktop.
Calculating the bearing to a coordinate/location from another coordinate/location in Dart using Math for Compass.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//This function takes two sets of values, say Coordinates and returns the angle bearing to the location. | |
//a1 and a2 being latitude and longitude of location a | |
//b1 and b2 being latitude and longitude of location b | |
double bearing(double a1, double a2, double b1, double b2) { | |
const double TWOPI = 6.2831853071795865; | |
const double RAD2DEG = 57.2957795130823209; | |
// if (a1 = b1 and a2 = b2) throw an error | |
//double theta = atan2(b1 - a1, a2 - b2); | |
double theta = math.atan2(b1 - a1, a2 - b2); | |
if (theta < 0.0) | |
theta += TWOPI; | |
return RAD2DEG * theta; | |
} | |
//Now when you want to use it in pointing/rotating your compass to a direction, you may use the Transform widget | |
Transform.rotate( | |
angle: ((_direction ?? 0) * (math.pi / 180) * -1) - (_adjustmentDirection / 180), //Divide the bearing value with 180 and subtract it from the compass value. | |
child: new Image.asset('lib/assets/qibla-compass.png', width: 250,), | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment