๐จ ์์ฒญ ํ๋ฆ
- JS๋ก ํ์ฌ ์๋, ๊ฒฝ๋ ๋ฐ์์ค๊ธฐ
- ๋ฐ์์จ ๊ฐ์ BackEnd ์๋ฒ๋ก ์ ๋ฌ
- Kakao Maps Api์ ์์ฒญ ๋ณด๋ด๊ธฐ
- ๋๋ก๋ช ์ฃผ์ ์ถ๋ ฅํ๊ธฐ
๐บ๏ธ ํ์ฌ ์๋, ๊ฒฝ๋ ๋ฐ์์ค๊ธฐ
JavaScript์์ ์ ๊ณตํด์ฃผ๋ ๊ธฐ๋ฅ ์ค geolocation์ ์ฌ์ฉ
geolocation
์ฐ์ geolocation์ ํจ์ ๋ชจ์๋ถํฐ ์ดํด๋ณด์!
navigator.geolocation.getCurrentPosition(successFn, failedFn);
function successFn(position) {
// ์๋
const lat = position.coords.latitude;
// ๊ฒฝ๋
const lng = position.coords.longitude;
// ์ฌ์ฉ์ ์ ์
}
function failedFn(positionError) {
// ์ฌ์ฉ์ ์ ์
}
์ ์ฝ๋์ฒ๋ผ ์ฑ๊ณตํ์ ๋๋ position์ ๋ฐ์์ ์ฌ์ฉ์ด ๊ฐ๋ฅํ๊ณ , ์คํจํ ๊ฒฝ์ฐ positionError๋ฅผ ๋งค๊ฐ๋ณ์๋ก ์ฌ์ฉํ ์ ์๊ฒ ๋๋ค.
๐ ๏ธ ์ฝ๋ ๊ตฌํ
function setLocation() {
navigator.geolocation.getCurrentPosition(function (position){
const lat = position.coords.latitude;
const lng = position.coords.longitude;
$('#location').attr('value', lat + "," + lng);
}, function (){
Swal.fire({
icon: 'question',
html: '์์น ์ ๋ณด๋ฅผ ๊ฐ์ ธ์ฌ ์ ์์ต๋๋ค.<br>์์น ์ ๋ณด ํ์ฉ์ ํด์ฃผ์ธ์.',
})
});
}
form์ ์๋, ๊ฒฝ๋์ ๋ํ ๋ฐ์ดํฐ๋ฅผ ๋ด์์ ๋ณด๋ผ ์์ ์ด๊ธฐ ๋๋ฌธ์ ์์ ๊ฐ์ด input์ ๊ฐ์ ์ถ๊ฐํด์ฃผ์๋ค.
์ฑ๊ณต, ์คํจ์ ๋ํ ํจ์๋ ๋ฐ๋ก ๋ ์ฌ์ฉํ ๊ฒ ๊ฐ์ง ์์์ ์ต๋ช
ํจ์๋ก ๋์ฒดํด ์ฌ์ฉํ๋ค.
๐น๏ธ ๋ฐ์์จ ๋ฐ์ดํฐ ์ฌ์ฉํ๊ธฐ
Controller
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestBody;
import lombok.RequiredArgsConstructor;
@RestController
@RequiredArgsConstructor
public class LocationController{
private final KakaoMapsApiRestTemplate template;
@PostMapping("/location")
public String getLocation(@RequestBody Map<String, Object> datum) {
String inputLocation = (String) datum.get("location");
String locationAddressName = template.getLocationAddressName(inputLocation);
return "...";
}
}
์ฌ์ฉ์๊ฐ ์
๋ ฅํ form์ ๊ฐ์ json ํํ๋ก ๋ฐ์์ค๊ธฐ ์ํด Map<String, Object> ํํ๋ก ๋ฐ์์จ๋ค.
๋ค๋ฅธ ๋ฐ์ดํฐ๋ ์ ์ธํ๊ณ location๋ง ์ฌ์ฉํ ์์ ์ด๊ธฐ ๋๋ฌธ์ Mapper๋ฅผ ์ฌ์ฉํ์ง ์๊ณ ๋ฐ๋ก ๊ฐ์ ธ์ ๋ฌธ์์ด ํํ๋ก ๋ณํํด์ค๋ค!
RestTemplate
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import java.util.Map;
@Component
public class KakaoMapsApiRestTemplate {
// properties์ ๋ฑ๋กํด๋์ Kakao Rest Api Key
@Value("${kakao.api.key}")
private String REST_API_KEY;
/**
* ํ์ฌ ์์น์ ๋ํ ์ ๋ณด๋ฅผ ๋ฐ์์ค๊ธฐ ์ํ ๋ฉ์๋
* @param currentLat JS๋ฅผ ํตํด ๋ฐ์์จ ์๋
* @param currentLng JS๋ฅผ ํตํด ๋ฐ์์จ ๊ฒฝ๋
* @return RestTemplate์ ํตํด ๋ฐ์์จ JSON(Map) ๊ฒฐ๊ณผ ๋ฐํ
* */
public Map<String, Object> getCurrentPosInfo(String currentLat, String currentLng) {
// ๋ฐ์์จ ์๋, ๊ฒฝ๋๋ฅผ ๊ฐ์ง๊ณ KakaoMapsApi์ ์กฐํ
String url = "https://dapi.kakao.com/v2/local/geo/coord2address.json?x={currentLng}&y={currentLat}";
return getRestTemplateResult(url, currentLat, currentLng);
}
/**
* Kakao Maps Api์ ์์ฒญ์ ๋ณด๋ด๊ธฐ ์ํ ํจ์
* @param currentLat ์ฌ์ฉ์์ ์๋
* @param currentLng ์ฌ์ฉ์์ ๊ฒฝ๋
* @return ์กฐํ๋ฅผ ํตํด ๋ฐ์์จ JSON(Map) ๊ฒฐ๊ณผ ๋ฐํ
* */
public Map<String, Object> getRestTemplateResult(String url, String currentLat, String currentLng) {
// Kakao Maps Api์ ์์ฒญ์ ๋ณด๋ด๊ธฐ ์ํ RestTemplate
RestTemplate restTemplate = new RestTemplate();
// ์์ฒญ header์ Authorization : KakaoAK key๋ฅผ ๋ด์ ์์ฒญ
HttpHeaders headers = new HttpHeaders();
// KakaoAK ๋์ด์ฐ๊ธฐ ์ฃผ์!!!
headers.set("Authorization", "KakaoAK " + REST_API_KEY);
// ์์ฒญ์ ๋ณด๋ธ ๋ค, ๋ฐ์์จ ๊ฐ์ response์ ์ ์ฅ
ResponseEntity<Map> response = restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(headers), Map.class, currentLng, currentLat);
return response.getBody();
}
/**
* JSONObject ํ์์ผ๋ก ๋ฐํ๋ ๋ฐ์ดํฐ ์ค์์ address_name๋ง ๋ฐ์์ค๋ ํจ์
* @param json RestTemplate ์์ฒญ์ ํตํด ๋ฐ์์ ๋ณํํ ๋ฐ์ดํฐ
* @return ์ฌ์ฉ์์ address_name์ ๋ฐํ
* */
public String getAddressName(JSONObject json) {
JSONObject address = (JSONObject) json.get("address");
return (String) address.get("address_name");
}
}
์ฝ๋์ ๊ธธ์ด๊ฐ ๋๋ฌด ๊ธธ์ด JSONObject๋ก ๋ณํํ๋ ๊ณผ์ ๊ณผ ์ผ๋ถ ํจ์๋ ๋ด์ง ๋ชปํ์ต๋๋ค.
์ ์ฝ๋์ ํ๋ฆ๋๋ก ์งํ๋๋ฉฐ, ํ์ฌ ์ฌ์ฉ์์ ์์น์ ๋๋ก๋ช ์ฃผ์๋ฅผ ๊ตฌํ ์ ์๋ค.
ํ์ฌ ์ขํ์ ๋ํ ์ฃผ์ ๋ง๊ณ ๋ ๋ ๋ค์ํ ๊ธฐ๋ฅ์ด ์์ผ๋ ์์ธํ ๋ด์ฉ์ ๊ณต์๋ฌธ์๋ฅผ ์ฐธ๊ณ ํ๋๊ฒ ์ข์ ๊ฒ ๊ฐ๋ค!
๐ค ํ๊ณ
์ด์ ์ Kakao Maps Api๋ฅผ ์ฌ์ฉํ ๋๋ ์ด๋ ต๋ค๊ณ ๋๊ปด์ก๋๋ฐ, ์๊ฐ์ด ์ง๋ ๋ค์ ์ฌ์ฉํด๋ณด๋ ์๊ฐ๋ณด๋ค ์ด๋ ต์ง ์์ ๋๋์ด ๋ค์๋ค.
์์ง์ JSON ํํ๋ฅผ ๋ฐ์์ ๋ณํํ๋ ๊ณผ์ ์ด ์ต์ํ์ง ์์ง๋ง, ๊ธ๋ฐฉ ๊ฐ์ ์ตํ ์ ์์ ๊ฒ ๊ฐ๋ค!
'Spring > ๊ธฐ๋ฅ ์ ๋ฆฌ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [Spring] - Spring Scheduler (0) | 2023.09.10 |
|---|---|
| [Spring] - AOP๋ฅผ ํ์ฉํ ๊ถํ ์ฌ๋ถ์ฌ (0) | 2023.09.10 |
| [Spring] - oEmbed๋ฅผ ํตํด ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ (1) | 2023.09.10 |
| [Spring] - Optional (1) | 2023.09.10 |
| [Spring] - Model Mapper (0) | 2023.09.10 |