Skip to content

Commit 9301d34

Browse files
committed
2 parents 3578fef + 21dc3f2 commit 9301d34

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed

src/main/java/com/example/log4u/domain/map/controller/MapController.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,78 @@
22

33
import java.util.List;
44

5+
import org.springframework.beans.factory.annotation.Value;
6+
import org.springframework.http.HttpEntity;
7+
import org.springframework.http.HttpHeaders;
8+
import org.springframework.http.HttpMethod;
59
import org.springframework.http.ResponseEntity;
10+
import org.springframework.security.core.annotation.AuthenticationPrincipal;
611
import org.springframework.web.bind.annotation.GetMapping;
12+
import org.springframework.web.bind.annotation.ModelAttribute;
713
import org.springframework.web.bind.annotation.RequestMapping;
814
import org.springframework.web.bind.annotation.RequestParam;
915
import org.springframework.web.bind.annotation.RestController;
16+
import org.springframework.web.client.RestTemplate;
17+
import org.springframework.web.util.UriComponentsBuilder;
1018

19+
import com.example.log4u.common.oauth2.dto.CustomOAuth2User;
20+
import com.example.log4u.domain.map.dto.MyLocationRequestDto;
21+
import com.example.log4u.domain.map.dto.ReverseGeocodingResponseDto;
1122
import com.example.log4u.domain.map.dto.response.DiaryClusterResponseDto;
1223
import com.example.log4u.domain.map.dto.response.DiaryMarkerResponseDto;
1324
import com.example.log4u.domain.map.service.MapService;
1425

1526
import io.swagger.v3.oas.annotations.tags.Tag;
1627
import lombok.RequiredArgsConstructor;
28+
import lombok.extern.slf4j.Slf4j;
1729

1830
@Tag(name = "지도 API")
1931
@RestController
2032
@RequiredArgsConstructor
2133
@RequestMapping("/maps")
34+
@Slf4j
2235
public class MapController {
2336

2437
private final MapService mapService;
2538

39+
@Value("${naver.api.client-id}")
40+
private String clientId;
41+
42+
@Value("${naver.api.client-secret}")
43+
private String secret;
44+
45+
private final RestTemplate restTemplate;
46+
47+
@GetMapping("/location")
48+
public ResponseEntity<ReverseGeocodingResponseDto> getMyLocation(
49+
@AuthenticationPrincipal CustomOAuth2User customOAuth2User,
50+
@ModelAttribute MyLocationRequestDto request
51+
) {
52+
53+
String naverMapsUrl = UriComponentsBuilder
54+
.fromHttpUrl("https://maps.apigw.ntruss.com/map-reversegeocode/v2/gc")
55+
.queryParam("coords", request.coords())
56+
.queryParam("output", request.output())
57+
.queryParam("orders", "legalcode,admcode,addr,roadaddr")
58+
.toUriString();
59+
60+
HttpHeaders headers = new HttpHeaders();
61+
headers.set("x-ncp-apigw-api-key-id", clientId);
62+
headers.set("x-ncp-apigw-api-key", secret);
63+
64+
HttpEntity<Void> entity = new HttpEntity<>(headers);
65+
66+
ResponseEntity<ReverseGeocodingResponseDto> response = restTemplate.exchange(
67+
naverMapsUrl,
68+
HttpMethod.GET,
69+
entity,
70+
ReverseGeocodingResponseDto.class
71+
);
72+
73+
log.debug("역 지오코딩 결과 : " + String.valueOf(response) + "\n");
74+
return ResponseEntity.ok(response.getBody());
75+
}
76+
2677
@GetMapping("/diaries/cluster")
2778
public ResponseEntity<List<DiaryClusterResponseDto>> getDiaryClusters(
2879
@RequestParam double south,
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.example.log4u.domain.map.dto;
2+
3+
public record MyLocationRequestDto(
4+
String coords,
5+
String output
6+
) {
7+
8+
public MyLocationRequestDto {
9+
if (output == null || output.isBlank()) {
10+
output = "json";
11+
}
12+
}
13+
14+
public double getLongitude() {
15+
return Double.parseDouble(coords.split(",")[0]);
16+
}
17+
18+
public double getLatitude() {
19+
return Double.parseDouble(coords.split(",")[1]);
20+
}
21+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.example.log4u.domain.map.dto;
2+
3+
import java.util.List;
4+
5+
public record ReverseGeocodingResponseDto(
6+
List<RegionInfo> regions
7+
) {
8+
public record RegionInfo(
9+
Area area0,
10+
Area area1,
11+
Area area2,
12+
Area area3,
13+
Area area4
14+
) {
15+
}
16+
17+
public record Area(
18+
String name,
19+
Coords coords
20+
) {
21+
}
22+
23+
public record Coords(
24+
Center center
25+
) {
26+
}
27+
28+
public record Center(
29+
double x,
30+
double y
31+
) {
32+
}
33+
}
34+

0 commit comments

Comments
 (0)