Is there any way to find a specific direction with a Flutter compass. Does that compass indicate a specific long and lat coordinates?
Asked
Active
Viewed 3,279 times
3
-
check this: https://pub.dev/packages/flutter_compass – Salma Dec 14 '19 at 06:27
-
1Salma. I know about this. But i want compass to link with location so compass arrow should point to that place.. – Khaliq Izrail Dec 14 '19 at 10:37
-
1you should check this plugin https://pub.dev/packages/flutter_qiblah – medyas Feb 21 '20 at 20:23
1 Answers
4
This is the standard code for Qibla compasses I use in my apps to get the offset:
double getOffsetFromNorth(double currentLatitude, double currentLongitude,
double targetLatitude, double targetLongitude) {
var la_rad = radians(currentLatitude);
var lo_rad = radians(currentLongitude);
var de_la = radians(targetLatitude);
var de_lo = radians(targetLongitude);
var toDegrees = degrees(atan(sin(de_lo - lo_rad) /
((cos(la_rad) * tan(de_la)) - (sin(la_rad) * cos(de_lo - lo_rad)))));
if (la_rad > de_la) {
if ((lo_rad > de_lo || lo_rad < radians(-180.0) + de_lo) &&
toDegrees > 0.0 &&
toDegrees <= 90.0) {
toDegrees += 180.0;
} else if (lo_rad <= de_lo &&
lo_rad >= radians(-180.0) + de_lo &&
toDegrees > -90.0 &&
toDegrees < 0.0) {
toDegrees += 180.0;
}
}
if (la_rad < de_la) {
if ((lo_rad > de_lo || lo_rad < radians(-180.0) + de_lo) &&
toDegrees > 0.0 &&
toDegrees < 90.0) {
toDegrees += 180.0;
}
if (lo_rad <= de_lo &&
lo_rad >= radians(-180.0) + de_lo &&
toDegrees > -90.0 &&
toDegrees <= 0.0) {
toDegrees += 180.0;
}
}
return toDegrees;
}
then you add the offset to the flutter_compass rotation to get the offset for each sensor update and apply the result to the special header.

Ali Qanbari
- 2,923
- 1
- 12
- 28
-
3can you show me that how did you add the offset to flutter_compass??? – Khaliq Izrail Dec 14 '19 at 17:07
-
2i created a Flutter plugin that allows you to get the Qibla direction https://pub.dev/packages/flutter_qiblah – medyas Feb 21 '20 at 20:23
-