diff --git a/package.json b/package.json index 5e8162c..01e4840 100644 --- a/package.json +++ b/package.json @@ -21,9 +21,16 @@ "angularx-qrcode": "^15.0.1", "daisyui": "^2.49.0", "deepl": "^1.0.13", - "qrcode": "^1.5.1", + "geolib": "^3.3.3", + "nodemon": "^2.0.22", + "os-browserify": "^0.3.0", + "path-browserify": "^1.0.1", + "qrcode": "^1.5.3", "rxjs": "~7.5.0", + "sharp": "^0.32.0", + "stream-browserify": "^3.0.0", "tslib": "^2.3.0", + "util": "^0.12.5", "zone.js": "~0.12.0" }, "devDependencies": { diff --git a/src/app/detail/detail.component.html b/src/app/detail/detail.component.html index 2ae121d..fe6abf4 100644 --- a/src/app/detail/detail.component.html +++ b/src/app/detail/detail.component.html @@ -12,7 +12,7 @@ allowfullscreen> - + qr diff --git a/src/app/detail/detail.component.ts b/src/app/detail/detail.component.ts index 2783901..dac4187 100755 --- a/src/app/detail/detail.component.ts +++ b/src/app/detail/detail.component.ts @@ -1,6 +1,11 @@ import {Component, OnInit} from '@angular/core'; import {ActivatedRoute} from "@angular/router"; import {positionService} from "../service/position.service"; +import { getDistance } from 'geolib'; +// @ts-ignore +import * as sharp from 'sharp'; +import * as qrcode from 'qrcode'; +import * as canvas from 'canvas'; @Component({ selector: 'app-detail', @@ -11,6 +16,8 @@ export class DetailComponent implements OnInit { private location: string | undefined; private id: number | undefined; + private URLParams: any; + test = { name: 'SPAI', cordinates: '46.15187077044123,8.799829438699243', @@ -30,25 +37,19 @@ export class DetailComponent implements OnInit { constructor(private route: ActivatedRoute , private positionService: positionService) {} async ngOnInit(){ - this.route.params.subscribe(params => { - this.location = params['location']; - this.id = params['id']; - }) - console.log(this.location); - console.log(this.id); - console.log(this.embed); + this.URLParams = this.route.snapshot.url.slice(-2).map(segment => segment.path); + console.log(this.URLParams); // ["lugnao", "1"] + console.log("getting your location: wait..."); this.cord = await this.positionService.getLocation(); - console.log(this.cord); + console.log("location: ", this.cord); this.checkDistanceTimer(); } checkDistanceTimer() { //set interval - let lat2 = this.test.cordinates.split(",")[0]; - let lon2 = this.test.cordinates.split(",")[1]; let intervalID = setInterval(() => { if (this.showNav) { - this.distance = this.positionService.getDistanceBetweenCoordinates(this.cord.lat, this.cord.lat, +lat2, +lon2); + this.distance = this.positionService.getDistanceBetweenCoordinates(this.cord.lat, this.cord.lon, this.test.lat, this.test.lng); console.log(this.distance); if (this.distance == 0) { this.showNav = false; @@ -63,4 +64,60 @@ export class DetailComponent implements OnInit { } }, 1000); } + + async generateQRCodeImage(url: string): Promise { + // Crea il QR code + const qrCode = await qrcode.toDataURL(url, { errorCorrectionLevel: "H" }); + + // Crea il canvas + const canvasInstance = canvas.createCanvas(300, 300); + const ctx = canvasInstance.getContext("2d"); + + // Carica il QR code nell'immagine + const qrCodeImage = await canvas.loadImage(qrCode); + + // Disegna il QR code nell'immagine + ctx.drawImage(qrCodeImage, 0, 0, 300, 300); + + // Ritorna l'immagine come buffer + return canvasInstance.toBuffer(); + } + + /*generateQR() { + console.log("generating QR code"); + let url = `http://localhost:4200/location/${this.URLParams[0]}/${this.URLParams[1]}`; + //this.addQRCodeToImage(url, `assets/testDetail/img.png`, `assets/images/${url}.png`); + console.log(url) + }*/ + + /*async addQRCodeToImage(url: string, imagePath: string, outputPath: string): Promise { + // Generate QR code + const qrCode = await qrcode.toBuffer(url); + + // Load input image using Sharp + const image = sharp(imagePath); + + // Get input image dimensions + const { width, height } = await image.metadata(); + + // Resize QR code to 25% of input image height + const qrCodeHeight = Math.round(height * 0.25); + const qrCodeBuffer = await sharp(qrCode) + .resize(qrCodeHeight, qrCodeHeight) + .toBuffer(); + + // Composite QR code onto input image at bottom-right corner + await image.composite([ + { + input: qrCodeBuffer, + gravity: 'southeast', + top: height - qrCodeHeight, + left: width - qrCodeHeight, + }, + ]); + + // Save output image to file + await image.toFile(outputPath); + }*/ + } diff --git a/src/app/service/position.service.ts b/src/app/service/position.service.ts index 2379f5b..8035142 100644 --- a/src/app/service/position.service.ts +++ b/src/app/service/position.service.ts @@ -47,18 +47,20 @@ export class positionService{ } async getLocation(): Promise { - console.log('get location'); return new Promise((resolve, reject) => { if (navigator.geolocation) { + const options = { + enableHighAccuracy: true + }; navigator.geolocation.getCurrentPosition((position) => { const lat = position.coords.latitude; const lon = position.coords.longitude; this.cord = { lat, lon }; - console.log(this.cord); + console.log("cordinate: ", this.cord); resolve(this.cord); }, (error) => { reject(error); - }); + }, options); } else { reject('Geolocation is not supported by this browser.'); } diff --git a/src/assets/testDetail/img.png b/src/assets/testDetail/img.png new file mode 100644 index 0000000..b9cba7d Binary files /dev/null and b/src/assets/testDetail/img.png differ