[JS] ์นด์นด์˜ค ์ขŒํ‘œ>์ฃผ์†Œ๋ณ€ํ™˜ API ์‚ฌ์šฉ
728x90

HTML5์—์„œ ์ œ๊ณตํ•˜๋Š” geolocation API๋ฅผ ํ†ตํ•ด ์œ„์น˜ ๊ฒ€์ƒ‰์„ ํ—ˆ์šฉํ•˜๋ฉด ํ˜„์žฌ ์ขŒํ‘œ๋ฅผ ์–ป์–ด๋‚ธ๋‹ค.

	function getLocation() {
		if (navigator.geolocation) {
			navigator.geolocation.getCurrentPosition(function(position) {
				let lon = position.coords.latitude; //์œ„๋„
				let lat = position.coords.longitude; //๊ฒฝ๋„
				toAddress(lon,lat);
		    }, function(error) {
		      console.error(error);
		    }, {
		      enableHighAccuracy: false,
		      maximumAge: 0,
		      timeout: Infinity
		    });
		}
	}

 

์—ฌ๊ธฐ์„œ ์–ป์–ด๋‚ธ ์ขŒํ‘œ๋ฅผ ์นด์นด์˜ค API๋ฅผ ์ด์šฉํ•ด ์‹œ,๋„ ๋‹จ์œ„์˜ ์œ„์น˜๋ฅผ ๋ฝ‘์•„๋‚ด๊ณ  ์‹ถ์—ˆ๋‹ค.

https://developers.kakao.com/docs/latest/ko/local/dev-guide

 

Kakao Developers

์นด์นด์˜ค API๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋‹ค์–‘ํ•œ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๊ฐœ๋ฐœํ•ด๋ณด์„ธ์š”. ์นด์นด์˜ค ๋กœ๊ทธ์ธ, ๋ฉ”์‹œ์ง€ ๋ณด๋‚ด๊ธฐ, ์นœ๊ตฌ API, ์ธ๊ณต์ง€๋Šฅ API ๋“ฑ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

developers.kakao.com

 

์นด์นด์˜ค ๋””๋ฒจ๋กœํผ ์‚ฌ์ดํŠธ์—์„œ ์ขŒํ‘œ๋กœ ํ–‰์ •๊ตฌ์—ญ์ •๋ณด ๋ฐ›๊ธฐ ๋ถ€๋ถ„์„ ์ฐธ๊ณ ํ•ด์•ผ ํ•œ๋‹ค. 

 

์ ์šฉํ•œ ๋‚˜์˜ ์ฝ”๋“œ (๋ถ€์กฑํ•จ์ด ๋งŽ๋‹ค..) 

function toAddress(lon,lat){
		console.log(`lon: ${lon}, lat: ${lat}`);
		$.ajax({
			url : `https://dapi.kakao.com/v2/local/geo/coord2address.json?x=${lat}&y=${lon}&input_coord=WGS84`,
		    type : 'GET',
		    headers : {
		      'Authorization' : '๋‚˜์˜ ์นด์นด์˜ค API ์ฝ”๋“œ'
		    },
		    success : function(result) {
				let totatlCount = result.meta.total_count; //์ด ๋ฌธ์„œ ์ˆ˜ 
		    	 if (totatlCount > 0) {
		            if (result.documents[0].road_address === null) {
		                addressName = result.documents[0].address.region_1depth_name; //์ง€์—ญ(์‹œ) ์ด๋ฆ„ 
		            } else {
		                addressName = result.documents[0].road_address.region_1depth_name;
		            }
		        }
		        addr = addressName;
		    },
		    error : function(e) {
		      console.log(e);
		    }
		});
	}

 

์ค‘์š”ํ•œ ๊ฒƒ์€ url์—์„œ ์œ„๋„, ๊ฒฝ๋„ ์ˆœ์œผ๋กœ ์˜ค๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๊ฒฝ๋„, ์œ„๋„ ์ˆœ์œผ๋กœ ์˜ค๋Š” ๊ฒƒ์ด๋‹ค!

https://dapi.kakao.com/v2/local/geo/coord2address.json?x=${lat}&y=${lon}&input_coord=WGS84

์ด๊ฒƒ ๋•Œ๋ฌธ์— ๋ช‡์‹œ๊ฐ„์„ ๋” ์‚ฝ์งˆํ–ˆ๋‹ค. ๐Ÿ˜‚๐Ÿ˜‚

 

html ์š”์†Œ์— ์ด๋ฒคํŠธ๋ฅผ ๊ฑธ์–ด์ฃผ์–ด ํ•ด๋‹น ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋  ์ˆ˜ ์žˆ๋„๋ก ํ–ˆ๋‹ค. 

 

 

 


๐Ÿ“ƒ์ฐธ๊ณ 

https://developer.mozilla.org/ko/docs/Web/API/Geolocation_API/Using_the_Geolocation_API

 

Geolocation API ์‚ฌ์šฉํ•˜๊ธฐ - Web API | MDN

Geolocation API๋Š” navigator.geolocation ๊ฐ์ฒด๋ฅผ ํ†ตํ•ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

developer.mozilla.org

 

https://velog.io/@lsj8367/JavaScript-%EC%A2%8C%ED%91%9C-%EC%A3%BC%EC%86%8C-%EB%B3%80%ED%99%98%EA%B3%84

 

[JavaScript] GeoLocation api, ์นด์นด์˜ค ์ขŒํ‘œ -> ์ฃผ์†Œ ๋ณ€ํ™˜๊ณ„ ๊ตฌํ˜„ํ•˜๊ธฐ

GeoLocation API GeoLocation API๋Š” navigator.geolocation ๊ฐ์ฒด๋ฅผ ํ†ตํ•ด ์‚ฌ์šฉ์„ ํ•  ์ˆ˜๊ฐ€ ์žˆ๋‹ค.

velog.io

 

 

** ์ฃผ๋‹ˆ์–ด๋ผ ๋ถ€์กฑํ•จ์ด ๋งŽ์Šต๋‹ˆ๋‹ค. ์ž˜๋ชป๋œ ๋ถ€๋ถ„์ด ์žˆ๋‹ค๋ฉด ์ง€์ ์€ ๊ฐ์‚ฌํžˆ ๋ฐ›๊ฒ ์Šต๋‹ˆ๋‹ค!

728x90