Fix cast problem in distance calculation.
This commit is contained in:
parent
91efe32482
commit
f15b01abcd
|
|
@ -76,7 +76,7 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
|
||||||
private TextView textViewCurrentLocation;
|
private TextView textViewCurrentLocation;
|
||||||
|
|
||||||
private MyLocation lastLocation = null;
|
private MyLocation lastLocation = null;
|
||||||
private int distance = 0;
|
private float distance = 0;
|
||||||
|
|
||||||
@RequiresApi(api = Build.VERSION_CODES.M)
|
@RequiresApi(api = Build.VERSION_CODES.M)
|
||||||
protected void checkPermissions() {
|
protected void checkPermissions() {
|
||||||
|
|
@ -505,7 +505,7 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateDistance() {
|
private void updateDistance() {
|
||||||
this.textViewCurrentLocation.setText(String.format("Distance: %,dm", distance));
|
this.textViewCurrentLocation.setText(String.format("Distance: %,dm", ((int) distance)));
|
||||||
this.textViewCurrentLocation.setVisibility(View.VISIBLE);
|
this.textViewCurrentLocation.setVisibility(View.VISIBLE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -75,7 +75,7 @@ public class MyLocation {
|
||||||
return this.bearing;
|
return this.bearing;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int distanceTo(MyLocation loc) {
|
public float distanceTo(MyLocation loc) {
|
||||||
double earthRadius = 6371000; //meters
|
double earthRadius = 6371000; //meters
|
||||||
double dLat = Math.toRadians(loc.latitude-latitude);
|
double dLat = Math.toRadians(loc.latitude-latitude);
|
||||||
double dLng = Math.toRadians(loc.longitude-longitude);
|
double dLng = Math.toRadians(loc.longitude-longitude);
|
||||||
|
|
@ -83,9 +83,8 @@ public class MyLocation {
|
||||||
Math.cos(Math.toRadians(latitude)) * Math.cos(Math.toRadians(loc.latitude)) *
|
Math.cos(Math.toRadians(latitude)) * Math.cos(Math.toRadians(loc.latitude)) *
|
||||||
Math.sin(dLng/2) * Math.sin(dLng/2);
|
Math.sin(dLng/2) * Math.sin(dLng/2);
|
||||||
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
|
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
|
||||||
float dist = (float) (earthRadius * c);
|
|
||||||
|
|
||||||
return (int) dist;
|
return (float) (earthRadius * c);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue