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

# Conflicts:
#	src/app/home/home.component.html
#	src/app/service/readjson.service.ts
This commit is contained in:
2023-04-06 14:30:25 +02:00
5 changed files with 111 additions and 114 deletions

View File

@@ -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)
}
/// ///
} }

View File

@@ -8,14 +8,14 @@
(keydown)="selezionaSuggerimento($event)" placeholder="Type here" (keydown)="selezionaSuggerimento($event)" placeholder="Type here"
class="input input-bordered input-primary w-full max-w-xs"> class="input input-bordered input-primary w-full max-w-xs">
<ng-container *ngIf="luoghiPopup | async as luoghi;"> <ng-container *ngIf="locationsPopup | async as locations;">
<ul *ngIf="luoghi.length > 0" class="menu bg-base-200 !w-fit p-2 rounded-box" id="list"> <ul *ngIf="locations.length > 0" class="menu bg-base-200 !w-fit p-2 rounded-box" id="list">
<li class="menu-title"> <li class="menu-title">
<span>Places</span> <span>Places</span>
</li> </li>
<li *ngFor="let luogo of luoghi"> <li *ngFor="let luogo of locations" (click)="luogoSelezionato=luogo.location; cercaLuogo(luogo.location)" >
{{luogo.location}} {{luogo.location}}
</li> </li>

View File

@@ -1,5 +1,5 @@
import {AfterViewInit, Component, ElementRef, OnDestroy, OnInit, ViewChild} from '@angular/core'; import {AfterViewInit, Component, ElementRef, OnDestroy, OnInit, ViewChild} from '@angular/core';
import {distinctUntilChanged, fromEvent, Subject, Subscription} from "rxjs"; import {distinctUntilChanged, fromEvent, Observable, Subject, Subscription} from "rxjs";
import {ReadjsonService} from "../service/readjson.service"; import {ReadjsonService} from "../service/readjson.service";
import {Locations} from "../interface/data"; import {Locations} from "../interface/data";
import * as QRCode from 'qrcode'; import * as QRCode from 'qrcode';
@@ -19,70 +19,69 @@ export class HomeComponent implements OnInit, AfterViewInit, OnDestroy {
@ViewChild('myInput') myInput?: ElementRef; @ViewChild('myInput') myInput?: ElementRef;
@ViewChild('myCanvas') myCanvas?: ElementRef<HTMLCanvasElement>; @ViewChild('myCanvas') myCanvas?: ElementRef<HTMLCanvasElement>;
luoghiPopup: Subject<Luogo[]> = new Subject<Luogo[]>() public locationsPopup: Subject<Locations[]> = new Subject<Locations[]>()
subs: Subscription[] = [] subs: Subscription[] = []
latitude: number | undefined;
longitude: number | undefined;
backgroundColor: string | undefined; backgroundColor: string | undefined;
qrCodeImage: string | undefined; qrCodeImage: string | undefined;
locations: Locations[] = [ locations: Locations[] = [];
{location: 'Locarno', region: 'Ticino', lat: 46.1704, lon: 8.7931},
{location: 'Lugano', region: 'Ticino', lat: 46.0037, lon: 8.9511},
{location: 'Luzern', region: 'Luzern', lat: 47.0502, lon: 8.3093},
{location: 'Lauterbrunnen', region: 'Bern', lat: 46.5939, lon: 7.9085}
];
luoghi: Luogo[] = [
{location: 'Locarno', lat: 46.1704, lon: 8.7931},
{location: 'Lugano', lat: 46.0037, lon: 8.9511},
{location: 'Luzern', lat: 47.0502, lon: 8.3093},
{location: 'Lauterbrunnen', lat: 46.5939, lon: 7.9085}
];
locationsFiltrati: Locations[] = []; locationsFiltrati: Locations[] = [];
luoghiFiltrati: Luogo[] = [];
luogoSelezionato: string = ''; luogoSelezionato: string = '';
suggerimentoAttivo: boolean = false; suggerimentoAttivo: boolean = false;
suggerimento: string = ''; suggerimento: string = '';
completamento: string = 'ciao'; completamento: string = 'ciao';
@ViewChild('myInput') myInput?: ElementRef;
@ViewChild('canvas') canvasRef: ElementRef | undefined; @ViewChild('canvas') canvasRef: ElementRef | undefined;
canvas: any; canvas: any;
ctx: any; ctx: any;
img: any; img: any;
constructor(private service: ReadjsonService) {} constructor(private readjsonService: ReadjsonService) {
}
ngOnInit(): void { ngOnInit(): void {
this.readjsonService.getLocations().subscribe(data => {
for (let i = 0; i < data.length; i++) {
this.locations.push(<Locations>data[i])
console.log(data[i])
}
});
console.log("home init"); console.log("home init");
this.subs.push(this.service.getLocation("Lugano").subscribe(val => console.log(val))) this.subs.push(this.readjsonService.getLocation("Lugano").subscribe(val => console.log(val)))
const text = 'https://aramisgrata.ch'; // sostituisci con la tua stringa const text = 'https://aramisgrata.ch'; // sostituisci con la tua stringa
QRCode.toDataURL(text, { errorCorrectionLevel: 'H' }, (err, url) => { QRCode.toDataURL(text, {errorCorrectionLevel: 'H'}, (err, url) => {
this.qrCodeImage = url; this.qrCodeImage = url;
}); });
} }
ngOnDestroy() {
this.subs.forEach(sub => sub.unsubscribe())
}
ngAfterViewInit() { ngAfterViewInit() {
console.log("canvas", this.myCanvas?.nativeElement);
const canvas = this.myCanvas?.nativeElement; const canvas = this.myCanvas?.nativeElement;
console.log("canvas 2", canvas);
if (canvas) if (canvas)
this.animateClouds(canvas); this.animateClouds(canvas);
if (this.locations != undefined) {
fromEvent(this.myInput?.nativeElement, 'focus').pipe( fromEvent(this.myInput?.nativeElement, 'focus').pipe(
// debounceTime(500), decommentarlo se bisogna fare una chiamata http // debounceTime(500), decommentarlo se bisogna fare una chiamata http
distinctUntilChanged() distinctUntilChanged()
).subscribe((val: any) => { ).subscribe((val: any) => {
this.luoghiPopup.next(this.locations.filter(l => l.location.toLowerCase().startsWith(val.target.value.toLowerCase()) this.locationsPopup.next(this.locations.filter(l => l.location.toLowerCase().startsWith(val.target.value.toLowerCase())))
))}) })
}
this.canvas = this.canvasRef?.nativeElement; this.canvas = this.canvasRef?.nativeElement;
if (this.canvas) {
this.ctx = this.canvas.getContext('2d'); this.ctx = this.canvas.getContext('2d');
this.img = new Image(); this.img = new Image();
@@ -100,24 +99,20 @@ export class HomeComponent implements OnInit, AfterViewInit, OnDestroy {
this.ctx.drawImage(qrCode, x, y, qrCodeSize, qrCodeSize); this.ctx.drawImage(qrCode, x, y, qrCodeSize, qrCodeSize);
} }
} }
this.img.src = 'src/assets/img/mountains.png';
this.img.src = 'src/assets/img/mountains.png';
}
fromEvent(this.myInput?.nativeElement, 'input') fromEvent(this.myInput?.nativeElement, 'input')
.pipe( .pipe(
// debounceTime(500), decommentarlo se bisogna fare una chiamata http // debounceTime(500), decommentarlo se bisogna fare una chiamata http
distinctUntilChanged() distinctUntilChanged()
).subscribe((val: any) => { ).subscribe((val: any) => {
this.luoghiPopup.next(this.luoghi.filter(l => l.location.toLowerCase().startsWith(val.target.value.toLowerCase()))) this.locationsPopup.next(this.locations.filter(l => l.location.toLowerCase().startsWith(val.target.value.toLowerCase())))
}) })
} }
ngOnDestroy() {
this.subs.forEach(sub => sub.unsubscribe())
}
animateClouds(canvas: HTMLCanvasElement): void { animateClouds(canvas: HTMLCanvasElement): void {
console.log("animating clouds")
let x = -200; let x = -200;
let y = 100; let y = 100;
let speed = 2; let speed = 2;
@@ -138,7 +133,6 @@ export class HomeComponent implements OnInit, AfterViewInit, OnDestroy {
} }
drawCloud(x: number, y: number, ctx: CanvasRenderingContext2D) { drawCloud(x: number, y: number, ctx: CanvasRenderingContext2D) {
console.log("xy:"+ x, y)
ctx.beginPath(); ctx.beginPath();
ctx.arc(x, y, 50, 0, 2 * Math.PI); ctx.arc(x, y, 50, 0, 2 * Math.PI);
ctx.arc(x + 25, y - 25, 50, 0, 2 * Math.PI); ctx.arc(x + 25, y - 25, 50, 0, 2 * Math.PI);
@@ -148,11 +142,11 @@ export class HomeComponent implements OnInit, AfterViewInit, OnDestroy {
ctx.fill(); ctx.fill();
} }
cercaLuogo(locations: string){ cercaLuogo(locations: string) {
setTimeout(()=>{ setTimeout(() => {
}, 1000); }, 1000);
this.locationsFiltrati = this.locations.filter((l: Locations) => l.location.toLowerCase().startsWith(locations.toLowerCase())); this.locationsFiltrati = this.locations.filter((l: Locations) => l.location.toLowerCase().startsWith(locations.toLowerCase()));
if(this.locationsFiltrati.length > 0){ if (this.locationsFiltrati.length > 0) {
this.suggerimentoAttivo = true; this.suggerimentoAttivo = true;
this.suggerimento = this.locationsFiltrati[0].location; this.suggerimento = this.locationsFiltrati[0].location;
this.completamento = stringDifference(locations, this.suggerimento); this.completamento = stringDifference(locations, this.suggerimento);
@@ -163,13 +157,6 @@ export class HomeComponent implements OnInit, AfterViewInit, OnDestroy {
this.myInput?.nativeElement.focus(); this.myInput?.nativeElement.focus();
} }
consigliaSuggerimento(locations: string) {
if (this.suggerimentoAttivo) {
this.luogoSelezionato = locations + "-" + this.completamento;
this.suggerimento = '';
}
}
selezionaSuggerimento(event: KeyboardEvent) { selezionaSuggerimento(event: KeyboardEvent) {
if (event.key === 'Tab' || event.key === 'Enter') { if (event.key === 'Tab' || event.key === 'Enter') {
if (this.suggerimentoAttivo) { if (this.suggerimentoAttivo) {
@@ -185,6 +172,7 @@ export class HomeComponent implements OnInit, AfterViewInit, OnDestroy {
return null; return null;
} }
protected readonly Event = Event;
} }
function stringDifference(str1: string, str2: string): string { function stringDifference(str1: string, str2: string): string {
@@ -196,4 +184,3 @@ function stringDifference(str1: string, str2: string): string {
} }
return diff; return diff;
} }

View 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)
}
}

View File

@@ -38,16 +38,18 @@ export class ReadjsonService{
tap(data => console.log("data requested", data)) tap(data => console.log("data requested", data))
); );
} }
/*
getWaypoints(location: string, id: number): Observable<waypoint[]> {
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) : [];
}),
tap(data => console.log("data requested", data))
);
}
*/
// getWaypoints(location: string, id: number): Observable<waypoint[]> {
// 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) : [];
// }),
// tap(data => console.log("data requested", data))
// );
// }
} }