getDistanceBetweenCoordinates now response with 2 numbers after the decimal point
This commit is contained in:
@@ -7,26 +7,7 @@ import {Cord} from "../interface/cord";
|
||||
export class positionService{
|
||||
cord: Cord = { lat: 0, lon: 0 };
|
||||
|
||||
/*getDistanceBetweenCoordinates(lat1: number | undefined, lon1: number | undefined, lat2: number, lon2: number) {
|
||||
if (lat1 === undefined || lon1 === undefined) {
|
||||
console.log('lat1 or lon1 is undefined')
|
||||
return 0;
|
||||
}else{
|
||||
const earthRadius = 6371; // Radius of the earth in km
|
||||
const dLat = this.deg2rad(lat2 - lat1); // deg2rad below
|
||||
const dLon = this.deg2rad(lon2 - lon1);
|
||||
const a =
|
||||
Math.sin(dLat / 2) * Math.sin(dLat / 2) +
|
||||
Math.cos(this.deg2rad(lat1)) * Math.cos(this.deg2rad(lat2)) *
|
||||
Math.sin(dLon / 2) * Math.sin(dLon / 2)
|
||||
;
|
||||
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
|
||||
// Distance in km
|
||||
return earthRadius * c;
|
||||
}
|
||||
}*/
|
||||
|
||||
getDistanceBetweenCoordinates(lat1: number | undefined, lon1: number | undefined, lat2: number, lon2: number): number{
|
||||
getDistanceBetweenCoordinates(lat1: number | undefined, lon1: number | undefined, lat2: number, lon2: number): number {
|
||||
if (lat1 === undefined || lon1 === undefined) {
|
||||
console.log('lat1 or lon1 is undefined')
|
||||
return 0;
|
||||
@@ -38,10 +19,11 @@ export class positionService{
|
||||
Math.cos(this.deg2rad(lat1)) * Math.cos(this.deg2rad(lat2)) *
|
||||
Math.sin(dLon / 2) * Math.sin(dLon / 2);
|
||||
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
|
||||
return earthRadius * c;
|
||||
let num = earthRadius * c;
|
||||
let numString = num.toFixed(2);
|
||||
return parseFloat(numString);
|
||||
}
|
||||
|
||||
|
||||
deg2rad(deg: number) {
|
||||
return deg * (Math.PI / 180)
|
||||
}
|
||||
@@ -66,6 +48,4 @@ export class positionService{
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user