[JavaScript30] ๐Ÿ—บ 21. GeoLocation

18610 ๋‹จ์–ด JavaScriptjavascript30JavaScript

๐Ÿ—บ 21. GeoLocation

์ดˆ๊ธฐ ์ฝ”๋“œ

<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <svg class="arrow" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" x="0px" y="0px" viewBox="0 0 64 64" enable-background="new 0 0 64 64" xml:space="preserve"><g><path fill="#ffffff" d="M32,1.824C15.361,1.824,1.825,15.361,1.825,32c0,16.639,13.537,30.176,30.175,30.176   S62.175,48.639,62.175,32C62.175,15.361,48.639,1.824,32,1.824z M29.715,3.988h1.12l2.333,3.807V3.988h1.069v5.701h-1.155  ~ ์ค‘๋žต ~"/></g></g></svg>


    <h1 class="speed">
        <span class="speed-value">0</span>
        <span class="units">KM/H</span>
    </h1>
    <style>
        html {
        font-size: 100px;
        }
        
        body {
        margin: 0;
        font-family: sans-serif;
        min-height: 100vh;
        display: flex;
        justify-content: center;
        align-items: center;
        flex-direction: column;
        background:
            radial-gradient(black 15%, transparent 16%) 0 0,
            radial-gradient(black 15%, transparent 16%) 8px 8px,
            radial-gradient(rgba(255,255,255,.1) 15%, transparent 20%) 0 1px,
            radial-gradient(rgba(255,255,255,.1) 15%, transparent 20%) 8px 9px;
        background-color: #282828;
        background-size: 16px 16px;
        background-attachment: fixed;
        }

        .arrow {
        width: 250px;
        overflow: hidden;
        transition: all 0.2s;
        transform: rotate(0deg);
        display: inline-block;
        }

        h1 {
        color: white;
        font-weight: 100;
        font-size: 60px;
        display: flex;
        align-items: center;
        }

        .units {
        font-size: 15px;
        }
        /*Compass: https://thenounproject.com/search/?q=compass&i=592352*/
    </style>
</body>
</html>

์ดˆ๊ธฐ ํ™”๋ฉด

๐ŸŒ ์ƒˆ๋กœ ์•Œ๊ฒŒ ๋œ ๊ฒƒ

๐Ÿ‘‰ GeoLocation API

์‚ฌ์šฉ์ž๊ฐ€ ์›ํ•  ๊ฒฝ์šฐ ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์— ์œ„์น˜ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•  ์ˆ˜ ์žˆ๋Š” API

๐Ÿ‘‰ watchPosition()

์žฅ์น˜์˜ ์œ„์น˜๊ฐ€ ๋ฐ”๋€” ๋•Œ ๋งˆ๋‹ค ์ž๋™์œผ๋กœ ํ˜ธ์ถœํ•  ํ•จ์ˆ˜๋ฅผ ๋“ฑ๋กํ•  ๋•Œ ์‚ฌ์šฉ.
์„ ํƒ์ ์œผ๋กœ ์˜ค๋ฅ˜ ๋ฐœ์ƒ ์‹œ ์‚ฌ์šฉํ•  ํ•จ์ˆ˜๋„ ์ง€์ • ๊ฐ€๋Šฅ

navigator.geolocation.watchPosition(success[, error[, options]])

GeolocationCoordinates์—์„œ ์–ป์„ ์ˆ˜ ์žˆ๋Š” ์ •๋ณด

  • latitude : ์œ„๋„
  • longitude : ๊ฒฝ๋„
  • altitude : ๊ณ ๋„
  • accuracy : ์œ„๋„, ๊ฒฝ๋„ ์ •ํ™•๋„
  • altitudeAccuracy : ๊ณ ๋„ ์ •ํ™•๋„
  • heading : ์ด๋™ ๋ฐฉํ–ฅ
  • speed : ์žฅ์น˜ ์†๋„

์ฐธ๊ณ  :

๐ŸŒ ๊ณผ์ •

๐Ÿ‘‰ 0. ์‹คํ–‰ํ™˜๊ฒฝ ์„ธํŒ…

npm installํ•˜์—ฌ npm start๋ฅผ ํ•จ.

์ด ๋•Œ ๊ฐ•์˜์—์„œ๋Š” macํ™˜๊ฒฝ์—์„œ simulator๋ฅผ ์ด์šฉํ•˜์ง€๋งŒ, windowํ™˜๊ฒฝ์ด๊ธฐ ๋•Œ๋ฌธ์— ํœด๋Œ€ํฐ์œผ๋กœ ์‹คํ–‰.

ํ™•์žฅํ”„๋กœ๊ทธ๋žจ์ด๋‚˜ window ์‹œ๋ฎฌ๋ ˆ์ดํ„ฐ ๊ฐ™์€๊ฒŒ ์žˆ๋‚˜ ์ฐพ์•„๋ณด๋‹ค๊ฐ€ ํœด๋Œ€ํฐ์—์„œ ์‹คํ–‰ ํ•˜์˜€๋‹คํ•˜์—ฌ, ํœด๋Œ€ํฐ์œผ๋กœ ์‹คํ–‰.

์ฝ˜์†”์ฐฝ ํ™•์ธ์„ ์œ„ํ•ด ์•„๋ž˜ ๋งํฌ๋ฅผ ํ†ตํ•ด ํœด๋Œ€ํฐ์—์„œ ์‹คํ–‰ํ•œ ๊ฒƒ์„ ๋ธŒ๋ผ์šฐ์ €์—์„œ ํ™•์ธํ•จ.

https://backstreet-programmer.tistory.com/30

ํœด๋Œ€ํฐ ์ ‘์† ์‹œ external ๋งํฌ๋กœ ์ ‘์†!

๐Ÿ‘‰ 1. ๋ณ€์ˆ˜ ์„ ์–ธ

const arrow = document.querySelector('.arrow');
const speed = document.querySelector('.speed-value');

arrow์™€ speed์— ์š”์†Œ๋“ค์„ ์„ ํƒ.

๐Ÿ‘‰ 2. ํ˜„์žฌ ์œ„์น˜, ์†๋„ ๊ฐ’ ์–ป๋Š” ํ•จ์ˆ˜ ์ƒ์„ฑ ๋ฐ ์ถœ๋ ฅ.

navigator.geolocation.watchPosition((data)=>{
    console.log(data);
    speed.textContent = Math.round(data.coords.speed*100);
    arrow.style.transform = `rotate(${data.coords.heading}deg)`;
}, (err)=>{
    console.err(err); 
    alert('Hey! YOU GOTTA ALLOW THAT TO HAPPEN!!!');
});

๐Ÿ‘‰ watchPosition()

์ฝ˜์†”์— ๋„˜์–ด์˜ค๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ฐ์–ด๋ณด๋ฉด,

์ด ์‚ฌ์ง„์˜ heading๊ณผ speed๋ถ€๋ถ„์ด ์†๋„์™€ ๋ฐฉํ–ฅ์ด ๋œ๋‹ค.
(์ฒ˜์Œ์—” ๊ทธ๋ƒฅ ์‹œ๋ฎฌ๋ ˆ์ดํ„ฐ ๋ฐฉ๋ฒ•์„ ์ฐพ๋‹ค๊ฐ€ ์ธํ„ฐ๋„ท์œผ๋กœ ์ผœ์„œ ์–ด๋–ค ๋ฐ์ดํ„ฐ๊ฐ€ ๋„˜์–ด์˜ค๋Š”์ง€๋งŒ ํ™•์ธํ•ด์„œ null์ด ์ถœ๋ ฅ๋จ.)

data์˜ coords์•ˆ์— ์ •๋ณด๋“ค์ด ๋‹ด๊ฒจ ์žˆ๋‹ค.
๊ฐ•์˜์—์„œ๋Š” ๊ฐ’์ด ์ž˜๋‚˜์˜ค์ง€๋งŒ, ๋‚ด ํฐ์œผ๋กœ ์‹คํ–‰ ํ–ˆ์„ ๋•Œ 0.xxxxxx๊ฐ’์ด ๋‚˜์™€ *100์„ ํ•˜์—ฌ ์ผ๋‹จ ๊ฐ’์ด ํ‘œ์‹œ๋˜๋Š” ์ง€ ํ™•์ธ.

๋ฐฑํ‹ฑ(``)์„ ์ด์šฉํ•˜์—ฌ ๋„˜์–ด ์˜จ ๊ฐ’๋“ค์„ ์ด์šฉํ•ด ์†๋„๋ฅผ ์ถœ๋ ฅํ•˜๊ณ , heading๋งŒํผ rotate๋ฅผ ์ด์šฉํ•ด ๋Œ๋ฆฐ๋‹ค.

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ