From 9d38faaba47dc3038a2b7dfe1a484b809327fc15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joe=20Ku=CC=88ng?= Date: Sat, 15 Apr 2023 15:08:25 +0200 Subject: [PATCH 1/4] fix input on getDistanceBetweenCoordinates --- src/app/service/position.service.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/app/service/position.service.ts b/src/app/service/position.service.ts index 32f20cd..c741aa2 100644 --- a/src/app/service/position.service.ts +++ b/src/app/service/position.service.ts @@ -1,5 +1,5 @@ import {Injectable} from '@angular/core'; -import{Cord} from "../interface/cord"; +import {Cord} from "../interface/cord"; @Injectable({ providedIn: 'root' @@ -26,7 +26,7 @@ export class positionService{ } }*/ - getDistanceBetweenCoordinates(lat1: number | undefined, lon1: number | undefined, lat2: number, lon2: 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,8 +38,7 @@ 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)); - const distance = earthRadius * c; // Distanza in km - return distance; + return earthRadius * c; } From 0be4c2fadcbe88580490112f9f9eb4d413b35269 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joe=20Ku=CC=88ng?= Date: Sat, 15 Apr 2023 15:08:58 +0200 Subject: [PATCH 2/4] fix getWaypoints --- src/app/service/readjson.service.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/app/service/readjson.service.ts b/src/app/service/readjson.service.ts index b737271..da27017 100644 --- a/src/app/service/readjson.service.ts +++ b/src/app/service/readjson.service.ts @@ -39,18 +39,22 @@ export class ReadjsonService{ ); } -/* + getWaypoints(location: string, id: number): Observable { return this.locations.pipe( map((locations) => { const foundLocation: Locations | undefined = locations.find((loc: Locations) => loc.location === location); - return foundLocation ? foundLocation.waypoints.filter((way: waypoint) => way.id === id) : []; + if (foundLocation?.waypoints) { + return foundLocation ? foundLocation.waypoints.filter((way: waypoint) => way.id === id) : []; + } else { + return []; + } }), tap(data => console.log("data requested", data)) ); } - */ + } From e0950acce6eb6a5b940e6dd786e6d17617813ee2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joe=20Ku=CC=88ng?= Date: Sat, 15 Apr 2023 15:09:22 +0200 Subject: [PATCH 3/4] change --- src/app/interface/data.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app/interface/data.ts b/src/app/interface/data.ts index 04d2051..0566f35 100644 --- a/src/app/interface/data.ts +++ b/src/app/interface/data.ts @@ -4,7 +4,7 @@ export interface Locations { lat: number; lon: number; distance?: number; - waypoints?: waypoint[]; + waypoints: waypoint[]; } export interface waypoint { From 92c5db204d4f6e20f2a250ad8a3ea507b38ab45c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joe=20Ku=CC=88ng?= Date: Sat, 15 Apr 2023 15:11:31 +0200 Subject: [PATCH 4/4] adding waypoinst list on list.component --> in working --- src/app/list/list.component.html | 2 +- src/app/list/list.component.ts | 29 ++++++++++++++++++++--------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/app/list/list.component.html b/src/app/list/list.component.html index c2dfc07..2d02414 100644 --- a/src/app/list/list.component.html +++ b/src/app/list/list.component.html @@ -10,7 +10,7 @@

{{waypoinst.name}}

-

Distance: }

+

Distance: {{getDistance(waypoinst.lat, location.lon)}}

diff --git a/src/app/list/list.component.ts b/src/app/list/list.component.ts index 9b99dc6..4264bd7 100644 --- a/src/app/list/list.component.ts +++ b/src/app/list/list.component.ts @@ -1,9 +1,7 @@ import {Component, OnInit} from '@angular/core'; import {Locations} from "../interface/data"; import {ReadjsonService} from "../service/readjson.service"; -import {Observable} from "rxjs"; import {ActivatedRoute} from "@angular/router"; -import {Cord} from "../interface/cord"; import {positionService} from "../service/position.service"; @Component({ @@ -16,13 +14,14 @@ export class ListComponent implements OnInit { locations: Partial[] | undefined; location: Partial | undefined; - isNear: boolean = true; - positionCord: any; + isNear: boolean = true; + distance: number = 0; - constructor(private route: ActivatedRoute ,private readjsonService: ReadjsonService, private positionService: positionService) {} + constructor(private route: ActivatedRoute, private readjsonService: ReadjsonService, private positionService: positionService) { + } async ngOnInit() { this.route.params.subscribe(params => { @@ -37,7 +36,6 @@ export class ListComponent implements OnInit { }); } }); - this.positionCord = await this.readjsonService.getLocations(); this.setDistance(); } @@ -48,16 +46,29 @@ export class ListComponent implements OnInit { if (this.locations[i].location === this.locationParams) { this.location = this.locations[i]; console.log("Location trovata:", this.location); - this.isNear= false; + this.isNear = false; break; } } } } - private setDistance(): void{ - if (this.location && this.isNear){ + + private setDistance(): void { + if (this.location && this.isNear) { this.distance = this.positionService.getDistanceBetweenCoordinates(this.location.lat, this.location.lon, this.positionCord.lat, this.positionCord.lon); console.log("Distanza: " + this.distance + " km"); } } + + getDistance(latLocation: number | undefined, lonLocation: number | undefined): any { + setInterval(async () => { + this.positionCord = await this.positionService.getLocation(); + if (this.location) { + return this.positionService.getDistanceBetweenCoordinates(latLocation, lonLocation, this.positionCord.lat, this.positionCord.lon); + } else { + return 0; + } + }, 1000); + } + }