Merge remote-tracking branch 'origin/dev' into dev

This commit is contained in:
grata
2023-04-15 15:13:21 +02:00
5 changed files with 32 additions and 18 deletions

View File

@@ -4,7 +4,7 @@ export interface Locations {
lat: number; lat: number;
lon: number; lon: number;
distance?: number; distance?: number;
waypoints?: waypoint[]; waypoints: waypoint[];
} }
export interface waypoint { export interface waypoint {

View File

@@ -10,7 +10,7 @@
<h3> <h3>
{{waypoinst.name}} {{waypoinst.name}}
</h3> </h3>
<h4>Distance: }</h4> <h4>Distance: {{getDistance(waypoinst.lat, location.lon)}}</h4>
</div> </div>
</div> </div>

View File

@@ -1,9 +1,7 @@
import {Component, OnInit} from '@angular/core'; import {Component, OnInit} from '@angular/core';
import {Locations} from "../interface/data"; import {Locations} from "../interface/data";
import {ReadjsonService} from "../service/readjson.service"; import {ReadjsonService} from "../service/readjson.service";
import {Observable} from "rxjs";
import {ActivatedRoute} from "@angular/router"; import {ActivatedRoute} from "@angular/router";
import {Cord} from "../interface/cord";
import {positionService} from "../service/position.service"; import {positionService} from "../service/position.service";
@Component({ @Component({
@@ -16,13 +14,14 @@ export class ListComponent implements OnInit {
locations: Partial<Locations>[] | undefined; locations: Partial<Locations>[] | undefined;
location: Partial<Locations> | undefined; location: Partial<Locations> | undefined;
isNear: boolean = true;
positionCord: any; positionCord: any;
isNear: boolean = true;
distance: number = 0; 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() { async ngOnInit() {
this.route.params.subscribe(params => { this.route.params.subscribe(params => {
@@ -37,7 +36,6 @@ export class ListComponent implements OnInit {
}); });
} }
}); });
this.positionCord = await this.readjsonService.getLocations();
this.setDistance(); this.setDistance();
} }
@@ -54,10 +52,23 @@ export class ListComponent implements OnInit {
} }
} }
} }
private setDistance(): void { private setDistance(): void {
if (this.location && this.isNear) { if (this.location && this.isNear) {
this.distance = this.positionService.getDistanceBetweenCoordinates(this.location.lat, this.location.lon, this.positionCord.lat, this.positionCord.lon); this.distance = this.positionService.getDistanceBetweenCoordinates(this.location.lat, this.location.lon, this.positionCord.lat, this.positionCord.lon);
console.log("Distanza: " + this.distance + " km"); 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);
}
} }

View File

@@ -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) { if (lat1 === undefined || lon1 === undefined) {
console.log('lat1 or lon1 is undefined') console.log('lat1 or lon1 is undefined')
return 0; return 0;
@@ -38,8 +38,7 @@ export class positionService{
Math.cos(this.deg2rad(lat1)) * Math.cos(this.deg2rad(lat2)) * Math.cos(this.deg2rad(lat1)) * Math.cos(this.deg2rad(lat2)) *
Math.sin(dLon / 2) * Math.sin(dLon / 2); Math.sin(dLon / 2) * Math.sin(dLon / 2);
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
const distance = earthRadius * c; // Distanza in km return earthRadius * c;
return distance;
} }

View File

@@ -39,18 +39,22 @@ export class ReadjsonService{
); );
} }
/*
getWaypoints(location: string, id: number): Observable<waypoint[]> { getWaypoints(location: string, id: number): Observable<waypoint[]> {
return this.locations.pipe( return this.locations.pipe(
map((locations) => { map((locations) => {
const foundLocation: Locations | undefined = locations.find((loc: Locations) => loc.location === location); const foundLocation: Locations | undefined = locations.find((loc: Locations) => loc.location === location);
if (foundLocation?.waypoints) {
return foundLocation ? foundLocation.waypoints.filter((way: waypoint) => way.id === id) : []; return foundLocation ? foundLocation.waypoints.filter((way: waypoint) => way.id === id) : [];
} else {
return [];
}
}), }),
tap(data => console.log("data requested", data)) tap(data => console.log("data requested", data))
); );
} }
*/
} }