좌표 -> 주소 변경 & 두 지점간 자동차로 이동 시간 구하기: KakaoMap API 적용하기
REST API 키를 환경변수에 저장한다.
Kakao Map API 관련 메서드들을 정의할 파일을 만들어준다.
import { Injectable, Inject } from '@nestjs/common';
import { ConfigType } from '@nestjs/config';
import appConfig from '../../../config/app.config';
import axios from 'axios';
@Injectable()
export class KakaoMapService {
constructor(
@Inject(appConfig.KEY) private config: ConfigType<typeof appConfig>,
) {}
async convertCoordinatesToAddress(
latitude: number,
longitude: number,
): Promise<string> {
const url = `https://dapi.kakao.com/v2/local/geo/coord2address.json?x=${longitude}&y=${latitude}&input_coord=WGS84`;
const response = await axios.get(url, {
headers: {
Authorization: `KakaoAK ${this.config.kakaoApiKey}`,
},
});
const address = response.data.documents[0].address.address_name;
return address;
}
}
KakaoMapService
를 추가해준다.@Module({
imports: [TypeOrmModule.forFeature([Reports])],
controllers: [ReportsController],
providers: [ReportsService, ReportsRepository, KakaoMapService],
exports: [ReportsService, ReportsRepository],
})