api.js
3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
const express = require('express');
const router = express.Router();
const axios = require('axios');
router.get('/findLocation/:textLocation', async (req, res, next) => {
try {
let {textLocation} = req.params;
textLocation = decodeURIComponent(textLocation);
const result = await axios.get(`https://api.vworld.kr/req/address?service=address&request=getcoord&version=1.0&crs=epsg:4326&address=${encodeURIComponent(textLocation)}&refine=true&simple=false&format=json&type=road&key=${process.env.MAP_API_KEY}`);
const {response} = result.data;
const location = {
title: textLocation,
coordinate: {
longitude: parseFloat(response.result.point.x),
latitude: parseFloat(response.result.point.y),
}
};
return res.json({location});
} catch (e) {
console.error(e);
next(e);
}
});
router.post('/findKicks', async (req, res, next) => {
try {
const {startLocation, distance, serviceStates} = req.body;
let kickLocationsArray = [[], [], []];
if (serviceStates[1]) {
const result = await axios.get(`https://ext.flowerroad.ai/external/scooter/scooter_list?lat=${startLocation.coordinate.latitude}&lon=${startLocation.coordinate.longitude}&distance=${distance}`, {
headers: {
accept: 'application/json',
'x-api-key': process.env.KICK_API_KEY
}
});
const {data} = result.data;
if (data.length !== 0) {
kickLocationsArray[1] = data.map(kickLocation => {
return {
title: kickLocation.battery,
coordinate: {
longitude: parseFloat(kickLocation.lon),
latitude: parseFloat(kickLocation.lat),
},
}
});
}
}
return res.json({kickLocationsArray});
} catch (e) {
console.error(e);
next(e);
}
});
router.post('/setKickRoute', async (req, res, next) => {
try {
const {startLocation, endLocation, personalVelocity} = req.body;
const startLocationX = startLocation.coordinate.longitude;
const startLocationY = startLocation.coordinate.latitude;
const endLocationX = endLocation.coordinate.longitude;
const endLocationY = endLocation.coordinate.latitude;
const result = await axios.post('https://apis.openapi.sk.com/tmap/routes/pedestrian?version=1&format=json', {
"startX": startLocationX,
"startY": startLocationY,
"endX": endLocationX,
"endY": endLocationY,
"speed": personalVelocity,
"startName": "출발지",
"endName": "도착지",
"angle": 1,
"endPoiId": 334852,
"reqCoordType": "WGS84GEO",
"searchOption": 0,
"resCoordType": "WGS84GEO",
}, {
headers: {
accept: 'application/json',
'content-type': 'application/json; charset=UTF-8',
appKey: process.env.PATH_API_KEY,
}
});
const {features} = result.data;
const drawInfoArr = [];
for (let i = 0; i < features.length; i++) {
const geometry = features[i].geometry;
if (geometry.type === 'Point') {
drawInfoArr.push(geometry.coordinates);
} else {
geometry.coordinates.forEach(coordinate => drawInfoArr.push(coordinate));
}
}
const lane = drawInfoArr.map(v => {
return {longitude: v[0], latitude: v[1]}
});
const distance = features[0].properties.totalDistance;
const time = features[0].properties.totalTime / 60;
return res.json({lane, distance, time});
} catch (e) {
console.error(e);
next(e);
}
});
module.exports = router;