created service to calculate distance between two coordinates
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import {Component, OnInit} from '@angular/core';
|
import {Component, OnInit} from '@angular/core';
|
||||||
import {ActivatedRoute} from "@angular/router";
|
import {ActivatedRoute} from "@angular/router";
|
||||||
|
import {CalculateDistanceService} from "../service/calculateDistance.service";
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-detail',
|
selector: 'app-detail',
|
||||||
@@ -10,7 +11,7 @@ export class DetailComponent implements OnInit {
|
|||||||
private location: string | undefined;
|
private location: string | undefined;
|
||||||
private id: number | undefined;
|
private id: number | undefined;
|
||||||
|
|
||||||
constructor(private route: ActivatedRoute) {
|
constructor(private route: ActivatedRoute , private calculateDistanceService: CalculateDistanceService) {
|
||||||
}
|
}
|
||||||
|
|
||||||
ngOnInit(): void {
|
ngOnInit(): void {
|
||||||
@@ -65,7 +66,7 @@ export class DetailComponent implements OnInit {
|
|||||||
let lon2 = this.test.cordinates.split(",")[1];
|
let lon2 = this.test.cordinates.split(",")[1];
|
||||||
let intervalID = setInterval(() => {
|
let intervalID = setInterval(() => {
|
||||||
if (this.showNav) {
|
if (this.showNav) {
|
||||||
this.distance = this.getDistanceBetweenCoordinates(lat1, lon1, +lat2, +lon2);
|
this.distance = this.calculateDistanceService.getDistanceBetweenCoordinates(lat1, lon1, +lat2, +lon2);
|
||||||
console.log(this.distance);
|
console.log(this.distance);
|
||||||
if (this.distance == 0) {
|
if (this.distance == 0) {
|
||||||
this.showNav = false;
|
this.showNav = false;
|
||||||
@@ -82,23 +83,5 @@ export class DetailComponent implements OnInit {
|
|||||||
}, 1000);
|
}, 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
getDistanceBetweenCoordinates(lat1: number, lon1: number, lat2: number, lon2: number) {
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
deg2rad(deg: number) {
|
|
||||||
return deg * (Math.PI / 180)
|
|
||||||
}
|
|
||||||
|
|
||||||
///
|
///
|
||||||
}
|
}
|
||||||
|
|||||||
25
src/app/service/calculateDistance.service.ts
Normal file
25
src/app/service/calculateDistance.service.ts
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
import {Injectable} from '@angular/core';
|
||||||
|
|
||||||
|
@Injectable({
|
||||||
|
providedIn: 'root'
|
||||||
|
})
|
||||||
|
export class CalculateDistanceService{
|
||||||
|
getDistanceBetweenCoordinates(lat1: number, lon1: number, lat2: number, lon2: number) {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
deg2rad(deg: number) {
|
||||||
|
return deg * (Math.PI / 180)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user