wip
This commit is contained in:
25
src/services/CalculateDistanceBetweenCoords.ts
Normal file
25
src/services/CalculateDistanceBetweenCoords.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
interface Request {
|
||||
lat1: number,
|
||||
lng1: number,
|
||||
lat2: number,
|
||||
lng2: number
|
||||
}
|
||||
|
||||
function convertToRad(lat: number, lng: number) {
|
||||
let latRad = lat * (Math.PI / 180);
|
||||
let lngRad = lng * (Math.PI / 180);
|
||||
|
||||
return { latRad, lngRad };
|
||||
}
|
||||
|
||||
function CalculateDistanceBetweenCoords({ lat1, lng1, lat2, lng2 }: Request){
|
||||
let { latRad, lngRad } = convertToRad(lat1, lng1);
|
||||
let { latRad: lat2Rad, lngRad: lng2Rad } = convertToRad(lat2, lng2);
|
||||
|
||||
let d = Math.acos(Math.sin(latRad) * Math.sin(lat2Rad) + Math.cos(latRad) * Math.cos(lat2Rad) * Math.cos(lngRad - lng2Rad)) * 6371;
|
||||
|
||||
return d;
|
||||
}
|
||||
|
||||
|
||||
export default CalculateDistanceBetweenCoords;
|
||||
@@ -22,5 +22,23 @@ class GetCoordinatesByAddress{
|
||||
return response.data;
|
||||
}
|
||||
}
|
||||
// class GetCoordinatesByAddress{
|
||||
// public async execute({ address_to }: Request): Promise<any> {
|
||||
// // let endereco = address_to.replace(/[^a-z0-9+ ]/gi,'')
|
||||
// let endereco = address_to.replace(/[^a-z0-9+áàâãéèêíïóôõöúçñ ]/gi,'')
|
||||
// // console.log(endereco)
|
||||
// endereco = endereco.replace(/ /gi,'+')
|
||||
// endereco = endereco.replace(/\+\+/g, '+')
|
||||
// // console.log(endereco)
|
||||
// const querystring = require('querystring');
|
||||
// const response = await axios.get(`https://maps.googleapis.com/maps/api/geocode/json?address=${querystring.stringify(endereco)}key=`)
|
||||
// console.log(response.data)
|
||||
// if (!response.data || !response.data.length) {
|
||||
// throw new AppError('Não foi possível encontrar coordenadas para o endereço informado!', 400);
|
||||
// }
|
||||
|
||||
// return response.data;
|
||||
// }
|
||||
// }
|
||||
|
||||
export default GetCoordinatesByAddress;
|
||||
|
||||
Reference in New Issue
Block a user