Language/JavaScript (WEB)

๐Ÿ—บ๏ธ Geolocation API๋กœ ์œ„์น˜, ๋‚ ์”จ ์ •๋ณด๋ฅผ ์–ป์–ด์˜ค๊ธฐ

์ธํŒŒ_ 2021. 10. 5. 23:27

Geolocation

Geolocation API 

Geolocation API๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํ”„๋กœ๊ทธ๋žจ์ด ์‚ฌ์šฉ์ž์˜ ์‹ค์ œ ์œ„์น˜๋ฅผ ๋ธŒ๋ผ์šฐ์ €์—๊ฒŒ ์š”์ฒญํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ค€๋‹ค. 

๋ฌผ๋ก  ์ด๋Ÿฐ ์ •๋ณด๋“ค์€ ์ฃผ์š”ํ•œ ๊ฐœ์ธ ์ •๋ณด์™€ ๊ด€๋ จ๋˜์–ด ์žˆ์œผ๋ฏ€๋กœ, ๋ธŒ๋ผ์šฐ์ €์—์„œ๋Š” Geolocation API๊ฐ€ ๋ฌผ๋ฆฌ์ ์ธ ์œ„์น˜ ์ •๋ณด์— ์ ‘๊ทผํ•˜๊ธฐ ์ „์— ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํ”„๋กœ๊ทธ๋žจ์ด ํ•ญ์ƒ ์‚ฌ์šฉ์ž์˜ ํ—ˆ๋ฝ์„ ๋ฐ›๋„๋ก ํ•˜๊ณ  ์žˆ๋‹ค. 

Geolocation API๋Š” ํ™˜๊ฒฝ์— ๋”ฐ๋ผ GPS๋ฅผ ๋น„๋กฏํ•œ Wi-Fi, ํœด๋Œ€์ „ํ™”์˜ ๊ธฐ์ง€๊ตญ, IP ์ฃผ์†Œ ๋“ฑ์—์„œ ์œ„์น˜ ์ •๋ณด๋ฅผ ์•Œ์•„๋‚ธ๋‹ค. 

๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์ง€์›ํ•˜๋Š” Geolocation API๋Š”navigator.gelocation์œผ๋กœ ์ •์˜๋˜์–ด ์žˆ๋‹ค. 


Geolocation API ์‚ฌ์šฉ๋ฒ•

์ด ๊ฐ์ฒด์—๋Š” ๋‹ค์Œ ์„ธ๊ฐ€์ง€ ๋ฉ”์„œ๋“œ๊ฐ€ ์กด์žฌํ•œ๋‹ค.

 

navigator.geolocation.getCurrentPosition() 

- ์‚ฌ์šฉ์ž์˜ ํ˜„์žฌ ์œ„์น˜๋ฅผ ์š”์ฒญํ•œ๋‹ค.

 

Geolocation API๋Š” ๋น„๋™๊ธฐ์ ์œผ๋กœ ๋™์ž‘ํ•œ๋‹ค. 

์œ„์น˜๋ฅผ ์š”์ฒญํ•˜๋Š” ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ํ˜•ํƒœ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

navigator.geolocation.getCurrentPosition(function(pos) {
    console.log(pos);
    var latitude = pos.coords.latitude;
    var longitude = pos.coords.longitude;
    alert("ํ˜„์žฌ ์œ„์น˜๋Š” : " + latitude + ", "+ longitude);
});

Geolocation

์„ฑ๊ณต์ ์ธ geolocation ์š”์ฒญ์€ ์œ„๋„์™€ ๊ฒฝ๋„ ์™ธ์—๋„ ๋ฏธํ„ฐ ๋‹จ์œ„์˜ ์ •ํ™•๋„ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ด ์ •ํ™•๋„ ๊ฐ’์€ ํ•ด๋‹น ์ขŒํ‘œ์™€ ์–ผ๋งˆ๋‚˜ ๊ฐ€๊นŒ์šด์ง€๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค. 

 

Position ๊ฐ์ฒด์˜ ์ฃผ์š” ์†์„ฑ

 ์†์„ฑ ๊ฐœ์š” 
 coords.latitude ๊ณ ๋„ 
 coords.latitude ์œ„๋„ 
 coords.longitude ๊ฒฝ๋„ 
 coords.accuracy ์œ„๋„/๊ฒฝ๋„์˜ ์˜ค์ฐจ(m) 
 coords.altitudeAccuracy ๊ณ ๋„์˜ ์˜ค์ฐจ(m) 
 coords.heading ๋ฐฉ์œ„(๋„) 
 coords.speed ์†๋„(m/์ดˆ) 
 timestamp ์ทจ๋“ ๋‚ ์งœ (1970๋…„๋ถ€ํ„ฐ ๊ฒฝ๊ณผ ๋ฐ€๋ฆฌ์ดˆ) 

 

+ ์œ„์น˜ ๊ตฌํ•  ๋•Œ ์—๋Ÿฌ ์ฒ˜๋ฆฌ๋‚˜ ์˜ต์…˜ ์„ค์ •ํ•˜๊ธฐ

 getCurrentPosition()์€ ์ฒซ ๋ฒˆ์งธ ์ฝœ๋ฐฑ ์ธ์ž๋ฟ๋งŒ ์•„๋‹ˆ๋ผ, geolocation ์š”์ฒญ์ด ์‹คํŒจํ–ˆ์„ ๋•Œ ํ˜ธ์ถœ๋  ๋‘ ๋ฒˆ์งธ ์ฝœ๋ฐฑ ์ธ์ž๋ฅผ ์„ ํƒ์ ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. 

์—๋Ÿฌ์ฒ˜๋ฆฌ(์‹คํŒจ์ฝœ๋ฐฑ)๋Š” ์ธ์ˆ˜๋กœ PositionError๊ฐ์ฒด๋ฅผ ๋ฐ›๋Š”๋‹ค. ์ฝœ๋ฐฑํ•จ์ˆ˜์˜ ๋‚ด๋ถ€์—์„œ๋Š” ์ด ์ •๋ณด๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€ ํ‘œ์‹œ ๋“ฑ์„ ์ฒ˜๋ฆฌํ•œ๋‹ค.

navigator.geolocation.getCurrentPosition(showYourLocation, showErrorMsg); 

function showYourLocation(position) {  // ์„ฑ๊ณตํ–ˆ์„๋•Œ ์‹คํ–‰
    var userLat = position.coords.latitude;
    var userLng = position.coords.longitude;
}

function showErrorMsg(error) { // ์‹คํŒจํ–ˆ์„๋•Œ ์‹คํ–‰
    switch(error.code) {
        case error.PERMISSION_DENIED:
        loc.innerHTML = "์ด ๋ฌธ์žฅ์€ ์‚ฌ์šฉ์ž๊ฐ€ Geolocation API์˜ ์‚ฌ์šฉ ์š”์ฒญ์„ ๊ฑฐ๋ถ€ํ–ˆ์„ ๋•Œ ๋‚˜ํƒ€๋‚ฉ๋‹ˆ๋‹ค!"
        break;

        case error.POSITION_UNAVAILABLE:
        loc.innerHTML = "์ด ๋ฌธ์žฅ์€ ๊ฐ€์ ธ์˜จ ์œ„์น˜ ์ •๋ณด๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์„ ๋•Œ ๋‚˜ํƒ€๋‚ฉ๋‹ˆ๋‹ค!"
        break;

        case error.TIMEOUT:
        loc.innerHTML = "์ด ๋ฌธ์žฅ์€ ์œ„์น˜ ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜ค๊ธฐ ์œ„ํ•œ ์š”์ฒญ์ด ํ—ˆ์šฉ ์‹œ๊ฐ„์„ ์ดˆ๊ณผํ–ˆ์„ ๋•Œ ๋‚˜ํƒ€๋‚ฉ๋‹ˆ๋‹ค!"
        break;

        case error.UNKNOWN_ERROR:
        loc.innerHTML = "์ด ๋ฌธ์žฅ์€ ์•Œ ์ˆ˜ ์—†๋Š” ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ ๋‚˜ํƒ€๋‚ฉ๋‹ˆ๋‹ค!"
        break;
    }
}

 

PositionError ๊ฐ์ฒด์˜ ์ฃผ์š” ์†์„ฑ

 ์†์„ฑ ๊ฐœ์š” 
 code ์—๋Ÿฌ ์ฝ”๋“œ
 message ์ƒ์„ธ ์—๋Ÿฌ ๋ฉ”์‹œ์ง€

 

์ด ๋‘ ๋ฉ”์„œ๋“œ๋Š” ์„ฑ๊ณต ์ฝœ๋ฐฑ๊ณผ ์˜ค๋ฅ˜ ์ฝœ๋ฐฑ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์„ธ๋ฒˆ์žฌ ์„ ํƒ์  ์ธ์ž๋กœ ์˜ต์…˜ ๊ฐ์ฒด๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. 

์˜ต์…˜์—๋Š” ๋†’์€ ์ˆ˜์ค€์˜ ์œ„์น˜ ์ •ํ™•๋„๊ฐ€ ์š”๊ตฌ๋˜๋Š”์ง€, ์–ด๋–ค ์œ„์น˜ '์ƒํƒœ'๋ฅผ ํ—ˆ์šฉํ• ์ง€, ์‹œ์Šคํ…œ์ด ์–ผ๋งˆ๋‚˜ ์˜ค๋žœ ์‹œ๊ฐ„๋™์•ˆ ์œ„์น˜ ์ถ”์ ์„ ํ—ˆ์šฉํ• ์ง€๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. 

์˜ต์…˜์€ '์˜ต์…˜๋ช…: ๊ฐ’' ํ˜•์‹์˜ ํ•ด์‹œ๋กœ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋Š” ์˜ต์…˜์€ ์•„๋ž˜ ํ‘œ์™€ ๊ฐ™๋‹ค.

 ์˜ต์…˜ ๊ฐœ์š” 
 timeout ์œ„์น˜ ์ •๋ณด ์ฝ๊ธฐ ํƒ€์ž„์•„์›ƒ(๋ฐ€๋ฆฌ์ดˆ) 
 maximumAge ์œ„์น˜ ์ •๋ณด์˜ ์บ์‹œ ๊ธฐํ•œ (๋ฐ€๋ฆฌ์ดˆ). 0์ด๋ฉด ์บ์‹œ ์•ˆํ•จ(=ํ•ญ์ƒ ์ตœ์‹  ์ •๋ณด๋ฅผ ์–ป์Œ) 
 enableHighAccuracy ๋†’์€ ์ •ํ™•๋„์˜ ์œ„์น˜ ์ •๋ณด๋ฅผ ๊ตฌํ• ์ง€(true | false) 
enableHighAccuracy ์˜ต์…˜์„ true๋กœ ํ•œ ๊ฒฝ์šฐ ์Šค๋งˆํŠธํฐ ํ™˜๊ฒฝ์—์„œ๋Š” GPS์—์„œ ์œ„์น˜์ •๋ณด๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๊ณ  ํ•œ๋‹ค. ์ด๊ฒƒ์œผ๋กœ ์œ„์น˜ ์ •๋ณด์˜ ์ •ํ™•๋„๋Š” ํ–ฅ์ƒ๋˜์ง€๋งŒ ๊ทธ๋งŒํผ ๋ฐฐํ„ฐ๋ฆฌ์†Œ๋น„๋„ ๋น ๋ฅด๋‹ค.

 

navigator.geolocation.watchPosition()

- ํ˜„์žฌ ์œ„์น˜๋ฅผ ์š”์ฒญํ•˜๋Š” ๊ฒƒ์€ ๋™์ผํ•˜์ง€๋งŒ, ์ง€์†์ ์œผ๋กœ ํ™•์ธํ•˜์—ฌ ์‚ฌ์šฉ์ž์˜ ์œ„์น˜๊ฐ€ ๋ณ€๊ฒฝ๋  ๋•Œ๋งˆ๋‹ค ์ง€์ •๋œ ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

 

navigator.geolocation.clearWatch()

- ์‚ฌ์šฉ์ž์˜ ์œ„์น˜ ์ •๋ณด๋ฅผ ์ˆ˜์ง‘ํ•˜๋Š” ์ž‘์—…์„ ์ค‘๋‹จํ•˜๋‹ค. ์ด ๋ฉ”์„œ๋“œ์˜ ์ „๋‹ฌ์ธ์ž๋Š” watchPosition()์„ ํ˜ธ์ถœํ•œ ๋‹ค์Œ ๋ฐ˜ํ™˜๋ฐ›์€ ์ˆซ์ž ๊ฐ’(๊ฐ์‹œ๋ฅผ ์‹๋ณ„ํ•˜๊ธฐ ์œ„ํ•œ ID๊ฐ’)์ด์–ด์•ผ ํ•œ๋‹ค. 


Geolocation ์˜ˆ์ œ

 

๋‚˜์˜ ํ˜„์žฌ ์œ„์น˜์ •๋ณด ์ถœ๋ ฅํ•˜๊ธฐ

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <div id="myLocationInfo"></div>

</body>
</html>
/* ๋น„๋™๊ธฐ์ ์œผ๋กœ ํ˜„์žฌ ์œ„์น˜๋ฅผ ์•Œ์•„๋‚ด์–ด ์ง€์ •๋œ ์š”์†Œ์— ์ถœ๋ ฅํ•œ๋‹ค. */
function whereami(elt) {
    // ์ด ๊ฐ์ฒด๋ฅผ getCurrentPosition() ๋ฉ”์„œ๋“œ์˜ ์„ธ๋ฒˆ์งธ ์ธ์ž๋กœ ์ „๋‹ฌํ•œ๋‹ค.
    var options = {
        // ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ, ๋†’์€ ์ •ํ™•๋„์˜ ์œ„์น˜(์˜ˆ๋ฅผ ๋“ค์–ด, GPS ๋“ฑ) ๋ฅผ ์ฝ์–ด์˜ค๋ ค๋ฉด true๋กœ ์„ค์ •
        // ๊ทธ๋Ÿฌ๋‚˜ ์ด ๊ธฐ๋Šฅ์€ ๋ฐฐํ„ฐ๋ฆฌ ์ง€์† ์‹œ๊ฐ„์— ์˜ํ–ฅ์„ ๋ฏธ์นœ๋‹ค. 
        enableHighAccuracy: false, // ๋Œ€๋žต์ ์ธ ๊ฐ’์ด๋ผ๋„ ์ƒ๊ด€ ์—†์Œ: ๊ธฐ๋ณธ๊ฐ’

        // ์œ„์น˜ ์ •๋ณด๊ฐ€ ์ถฉ๋ถ„ํžˆ ์บ์‹œ๋˜์—ˆ์œผ๋ฉด, ์ด ํ”„๋กœํผํ‹ฐ๋ฅผ ์„ค์ •ํ•˜์ž, 
        // ์œ„์น˜ ์ •๋ณด๋ฅผ ๊ฐ•์ œ๋กœ ์žฌํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๊ธฐ๋„ ํ•˜๋Š” ์ด ๊ฐ’์˜ ๊ธฐ๋ณธ ๊ฐ’์€ 0์ด๋‹ค.
        maximumAge: 30000,     // 5๋ถ„์ด ์ง€๋‚˜๊ธฐ ์ „๊นŒ์ง€๋Š” ์ˆ˜์ •๋˜์ง€ ์•Š์•„๋„ ๋จ

        // ์œ„์น˜ ์ •๋ณด๋ฅผ ๋ฐ›๊ธฐ ์œ„ํ•ด ์–ผ๋งˆ๋‚˜ ์˜ค๋žซ๋™์•ˆ ๋Œ€๊ธฐํ•  ๊ฒƒ์ธ๊ฐ€?
        // ๊ธฐ๋ณธ๊ฐ’์€ Infinity์ด๋ฏ€๋กœ getCurrentPosition()์€ ๋ฌดํ•œ์ • ๋Œ€๊ธฐํ•œ๋‹ค.
        timeout: 15000    // 15์ดˆ ์ด์ƒ ๊ธฐ๋‹ค๋ฆฌ์ง€ ์•Š๋Š”๋‹ค.
    }

    if(navigator.geolocation) // geolocation ์„ ์ง€์›ํ•œ๋‹ค๋ฉด ์œ„์น˜๋ฅผ ์š”์ฒญํ•œ๋‹ค. 
        navigator.geolocation.getCurrentPosition(success, error, options);
    else
        elt.innerHTML = "์ด ๋ธŒ๋ผ์šฐ์ €์—์„œ๋Š” Geolocation์ด ์ง€์›๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.";


    // geolocation ์š”์ฒญ์ด ์‹คํŒจํ•˜๋ฉด ์ด ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.
    function error(e) {
        // ์˜ค๋ฅ˜ ๊ฐ์ฒด์—๋Š” ์ˆ˜์น˜ ์ฝ”๋“œ์™€ ํ…์ŠคํŠธ ๋ฉ”์‹œ์ง€๊ฐ€ ์กด์žฌํ•œ๋‹ค.
        // ์ฝ”๋“œ ๊ฐ’์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.
        // 1: ์‚ฌ์šฉ์ž๊ฐ€ ์œ„์น˜ ์ •๋ณด๋ฅผ ๊ณต์œ  ๊ถŒํ•œ์„ ์ œ๊ณตํ•˜์ง€ ์•Š์Œ.
        // 2: ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์œ„์น˜๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์—†์Œ.
        // 3: ํƒ€์ž„์•„์›ƒ์ด ๋ฐœ์ƒ๋จ.
        elt.innerHTML = "Geolocation ์˜ค๋ฅ˜ "+e.code +": " + e.message;
    }


    // geolocation ์š”์ฒญ์ด ์„ฑ๊ณตํ•˜๋ฉด ์ด ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋œ๋‹ค.
    function success(pos) {

        console.log(pos); // [๋””๋ฒ„๊น…] Position ๊ฐ์ฒด ๋‚ด์šฉ ํ™•์ธ

        // ํ•ญ์ƒ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋Š” ํ•„๋“œ๋“ค์ด๋‹ค. timestamp๋Š” coords ๊ฐ์ฒด ๋‚ด๋ถ€์— ์žˆ์ง€ ์•Š๊ณ , 
        // ์™ธ๋ถ€์—์„œ ๊ฐ€์ ธ์˜ค๋Š” ํ•„๋“œ๋ผ๋Š” ์ ์— ์ฃผ์˜ํ•˜๋‹ค. 
        var msg = "๋‹น์‹ ์€ " +
            new Date(pos.timestamp).toLocaleString() + "์— " +
            " ์œ„๋„ " + pos.coords.latitude + 
            " ๊ฒฝ๋„ " + pos.coords.longitude + "์—์„œ "+ 
            " ์•ฝ " + pos.coords.accuracy + " ๋ฏธํ„ฐ ๋–จ์–ด์ง„ ๊ณณ์— ์žˆ์Šต๋‹ˆ๋‹ค.";

        // ํ•ด๋‹น ๊ธฐ๊ธฐ๊ฐ€ ๊ณ ๋„ (altitude)๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉด, ํ•ด๋‹น ์ •๋ณด๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.
        if(pos.coords.altitude) {
            msg += " ๋‹น์‹ ์€ ํ•ด๋ฐœ " + pos.coords.altitude + " ± " + 
                pos.coords.altitudeAccuracy + " ๋ฏธํ„ฐ์— ์žˆ์Šต๋‹ˆ๋‹ค.";
        }

        // ํ•ด๋‹น ๊ธฐ๊ธฐ๊ฐ€ ์†๋„์™€ ๋ถ์ชฝ ๊ธฐ์ค€ ๊ฐ (heading)์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค๋ฉด ์—ญ์‹œ ์ถ”๊ฐ€ํ•ด์ค€๋‹ค.
        if(pos.coords.speed) {
            msg += " ๋‹น์‹ ์€ " + pos.coords.heading + " ๋ฐฉํ–ฅ์œผ๋กœ " +
                "์ดˆ์† " + pos.coords.speed + "(m/s)์˜ ์†๋„๋กœ ์›€์ง์ด๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.";
        }

        elt.innerHTML = msg;     // ๋ชจ๋“  ์œ„์น˜ ์ •๋ณด๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.
    }    
}


// ๋‚˜์˜ ์œ„์น˜์ •๋ณด๋ฅผ ์ถœ๋ ฅํ•  ๊ฐ์ฒด ๊ตฌํ•˜๊ธฐ
var elt = document.getElementById("myLocationInfo");

// ๋‚˜์˜ ์œ„์น˜์ •๋ณด ์ถœ๋ ฅํ•˜๊ธฐ
whereami(elt);

Geolocation


์ •๊ธฐ์ ์œผ๋กœ ์œ„์น˜ ์ •๋ณด ์–ป๊ธฐ

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="http://code.jquery.com/jquery-1.11.0.js"></script>
<script>
    $(function() {        
        // Geolocation API์— ์•ก์„ธ์Šคํ•  ์ˆ˜ ์žˆ๋Š”์ง€๋ฅผ ํ™•์ธ
        if (navigator.geolocation) {
            //์œ„์น˜ ์ •๋ณด๋ฅผ ์ •๊ธฐ์ ์œผ๋กœ ์–ป๊ธฐ
            var id = navigator.geolocation.watchPosition(
                    function(pos) {
                        $('#latitude').html(pos.coords.latitude);     // ์œ„๋„ 
                        $('#longitude').html(pos.coords.longitude); // ๊ฒฝ๋„ 
                    });
            
            // ๋ฒ„ํŠผ ํด๋ฆญ์œผ๋กœ ๊ฐ์‹œ๋ฅผ ์ค‘์ง€
            $('#btnStop').click(function() {
                navigator.geolocation.clearWatch(id);
            });
        } else {
            alert("์ด ๋ธŒ๋ผ์šฐ์ €์—์„œ๋Š” Geolocation์ด ์ง€์›๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.")
        }
        
    });
</script>
</head>
<body>
    <ul>
        <li>์œ„๋„:<span id="latitude"></span></li>
        <li>๊ฒฝ๋„:<span id="longitude"></span></li>
    </ul>
    <input id="btnStop" type="button" value="๊ฐ์‹œ๋ฅผ ๋๋‚ธ๋‹ค" />
</body>
</html>

Geolocation


๋‚ด ์œ„์น˜์˜ ๋‚ ์”จ, ์˜จ๋„ ๊ฐ€์ ธ์˜ค๊ธฐ

api๋ฅผ ์“ฐ๊ธฐ์œ„ํ•ด ํ•ด๋‹น ์›น์‚ฌ์ดํŠธ์— ๋“ค์–ด๊ฐ€์„œ ํšŒ์›๊ฐ€์ž… ํ•ด์ฃผ์ž. ๊ทธ๋ฆฌ๊ณ  api key๋ฅผ ์–ป์ž.

 

Weather API - OpenWeatherMap

Please, sign up to use our fast and easy-to-work weather APIs for free. In case your requirements go beyond our freemium account conditions, you may check the entire list of our subscription plans. You can read the How to Start guide and enjoy using our po

openweathermap.org

Geolocation
Geolocation

 

 

์šฐ๋ฆฌ๊ฐ€ ์‚ฌ์šฉํ•  api๋Š” Current Weather Data๋ผ๋Š” api์ด๋‹ค.

Geolocation

 

 

๋‹ค์–‘ํ•œ rest api์š”์ฒญ ๋“ค์ด ์žˆ๊ณ , ์šฐ๋ฆฌ๊ฐ€ ์‚ฌ์šฉํ• ๊ฒƒ์€, ๊ฒฝ๋„์™€ ์œ„๋„๋ฅผ ํ†ตํ•ด ํ•ด๋‹น ๋‚ ์”จ ์ •๋ณด๋ฅผ ์–ป๋Š” ๊ฒƒ์ด๋‹ค.

Geolocation

 

ํ•ด๋‹น uri๋กœ ๋“ค์–ด๊ฐ€๋ฉด jsonํ˜•ํƒœ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์–ป์„์ˆ˜ ์žˆ๋‹ค. ์šฐ๋ฆฌ๋Š” fetch๋ฅผ ํ†ตํ•ด ajaxํ†ต์‹ ์œผ๋กœ ํ•ด๋‹น json๋ฐ์ดํ„ฐ ๊ฐ’์„ ๊ฐ€์ ธ์™€ ์‚ฌ์šฉํ•  ๊ฒƒ์ด๋‹ค.

Geolocation

 

[์ตœ์ข… ์ฝ”๋“œ]

const API_KEY = '๋‹น์‹ ์˜ API ํ‚ค๋ฅผ ์—ฌ๊ธฐ์— ๊ธฐ์žฌํ•˜์„ธ์š”';

function onGeoOk(position) {
   const latitude = position.coords.latitude;
   const longitude = position.coords.longitude;

   // console.log(`You live in ${latitude} and ${longitude}`);

   fetch(
      `https://api.openweathermap.org/data/2.5/weather?lat=${latitude}&lon=${longitude}&appid=${API_KEY}&units=metric`,
   )
      .then(response => response.json())
      .then(data => console.log(`์˜จ๋„ : ${data.main.temp}, ๋‚ ์”จ : ${data.weather[0].main}`));
}

function onGeoError() {
   alert("Can't find you. No weather for you.");
}

navigator.geolocation.getCurrentPosition(onGeoOk, onGeoError);

Geolocation