Skip to content

Commit

Permalink
feat: 주소를 위경도로 변환하는 Geocode API 연동 및 구현
Browse files Browse the repository at this point in the history
closes: #41
  • Loading branch information
CODe5753 committed Feb 25, 2023
1 parent f776e28 commit 1c26422
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.ftw.hometerview.place.controller.dto;

import com.ftw.hometerview.place.domain.Company;
import com.ftw.hometerview.place.util.NaverMapGeocodeAPI.GeoPoint;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import lombok.Builder;
Expand All @@ -15,18 +16,14 @@ public static class RegisterCompany {
private String name;
@NotBlank
private String roadAddress;
@NotNull
private Double lat;
@NotNull
private Double lon;

public Company toCompany() {
public Company toCompany(GeoPoint geoPoint) {
return Company.builder()
.name(this.name)
.roadAddress(this.roadAddress) // oo시 oo구 oo동 ~~
.province(this.roadAddress.split(" ")[1])
.lat(this.lat)
.lon(this.lon)
.lat(geoPoint.getLat())
.lon(geoPoint.getLon())
.build();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.ftw.hometerview.place.controller.dto.CompanyDto;
import com.ftw.hometerview.place.domain.Company;
import com.ftw.hometerview.place.repository.company.CompanyRepository;
import com.ftw.hometerview.place.util.NaverMapGeocodeAPI;
import com.ftw.hometerview.place.util.NaverMapGeocodeAPI.GeoPoint;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
Expand All @@ -15,12 +17,16 @@
public class CompanyService {

private final CompanyRepository companyRepository;
private final NaverMapGeocodeAPI naverMapGeocodeAPI;

public void register(CompanyDto.RegisterCompany req) {
if (isDuplicated(req.getName(), req.getRoadAddress())) {
throw new BadRequestException(ResponseType.DATA_DUPLICATED);
}
Company company = req.toCompany();

GeoPoint geoPoint = naverMapGeocodeAPI.convert(req.getRoadAddress());

Company company = req.toCompany(geoPoint);
// TODO: get nearliest station from naver API
// TODO: get station id from db
company.setNearliestStation("");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.ftw.hometerview.place.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.List;
import java.util.Map;
import lombok.Builder;
import lombok.Getter;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;

@Log4j2
@Component
public class NaverMapGeocodeAPI {

@Builder
@Getter
public static class GeoPoint {

Double lat;
Double lon;
}

private static final String BASE_URL = "https://naveropenapi.apigw.ntruss.com";
private static final String PATH = "/map-geocode/v2/geocode";
@Value("${naver.api-key-id}")
private String API_KEY_ID;
@Value("${naver.api-key}")
private String API_KEY;

public GeoPoint convert(String address) {
WebClient client = WebClient.builder()
.baseUrl(BASE_URL)
.build();

ObjectMapper objectMapper = new ObjectMapper();

String responseJson = client.get()
.uri(uriBuilder -> uriBuilder
.path(PATH)
.queryParam("query", address)
.build())
.header("X-NCP-APIGW-API-KEY-ID", API_KEY_ID)
.header("X-NCP-APIGW-API-KEY", API_KEY)
.retrieve()
.bodyToMono(String.class)
.block();

Map<String, Object> responseMap = null;
try {
responseMap = objectMapper.readValue(responseJson, Map.class);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
List<Map<String, String>> addressesMap = (List<Map<String, String>>) responseMap.get(
"addresses");
Map<String, String> addressMap = addressesMap.get(0);
String lon = addressMap.get("x");
String lat = addressMap.get("y");
return GeoPoint.builder()
.lat(Double.parseDouble(lat))
.lon(Double.parseDouble(lon))
.build();
}
}

0 comments on commit 1c26422

Please sign in to comment.