This commit is contained in:
Hamza-Ayed
2026-03-20 02:14:50 +03:00
parent 65ce6a0ba2
commit 6e6ee55d03
47 changed files with 14706 additions and 0 deletions
+71
View File
@@ -0,0 +1,71 @@
import { Injectable, HttpException, HttpStatus } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import axios from 'axios';
@Injectable()
export class MapsService {
private readonly graphHopperUrl: string;
constructor(private configService: ConfigService) {
this.graphHopperUrl = this.configService.get('GRAPH_HOPPER_URL', 'http://map-routing:8080');
}
async getRoute(from: [number, number], to: [number, number]) {
// Proxy routing request to GraphHopper
// تحويل طلب حساب المسارم إلى GraphHopper
try {
const response = await axios.get(`${this.graphHopperUrl}/route`, {
params: {
point: [`${from[0]},${from[1]}`, `${to[0]},${to[1]}`],
profile: 'car',
locale: 'en',
calc_points: true,
points_encoded: false,
elevation: false,
type: 'json',
},
paramsSerializer: (params) => {
const searchParams = new URLSearchParams();
Object.entries(params).forEach(([key, value]) => {
if (Array.isArray(value)) {
value.forEach(v => searchParams.append(key, v));
} else {
searchParams.append(key, value as any);
}
});
return searchParams.toString();
},
});
console.log('Routing Successful:', response.request.path);
const route = response.data.paths[0];
if (!route) {
throw new HttpException('No route found', HttpStatus.NOT_FOUND);
}
return {
distance: route.distance, // In meters
duration: route.time / 1000, // In seconds
points: route.points.coordinates, // Array of [lng, lat]
bbox: route.bbox,
};
} catch (error) {
console.error('Routing Error:', error.message);
if (error.response) {
console.error('GraphHopper Response:', error.response.data);
}
throw new HttpException('Error calculating route', HttpStatus.BAD_GATEWAY);
}
}
async getMapConfig() {
// Return basic map configuration for clients
// إرجاع إعدادات الخريطة للواجهة الأمامية
return {
center: [31.95, 35.91], // Amman, Jordan
zoom: 12,
tileServerUrl: this.configService.get('TILE_SERVER_URL', 'http://localhost:3001'),
};
}
}