api.js 3.98 KB
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;