์ด๊ธฐ ์ฝ๋
<!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>
์ด๊ธฐ ํ๋ฉด
์ฌ์ฉ์๊ฐ ์ํ ๊ฒฝ์ฐ ์น ์ ํ๋ฆฌ์ผ์ด์ ์ ์์น ์ ๋ณด๋ฅผ ์ ๊ณตํ ์ ์๋ API
์ฅ์น์ ์์น๊ฐ ๋ฐ๋ ๋ ๋ง๋ค ์๋์ผ๋ก ํธ์ถํ ํจ์๋ฅผ ๋ฑ๋กํ ๋ ์ฌ์ฉ.
์ ํ์ ์ผ๋ก ์ค๋ฅ ๋ฐ์ ์ ์ฌ์ฉํ ํจ์๋ ์ง์ ๊ฐ๋ฅ
navigator.geolocation.watchPosition(success[, error[, options]])
GeolocationCoordinates์์ ์ป์ ์ ์๋ ์ ๋ณด
latitude
: ์๋longitude
: ๊ฒฝ๋altitude
: ๊ณ ๋accuracy
: ์๋, ๊ฒฝ๋ ์ ํ๋altitudeAccuracy
: ๊ณ ๋ ์ ํ๋heading
: ์ด๋ ๋ฐฉํฅspeed
: ์ฅ์น ์๋์ฐธ๊ณ :
npm installํ์ฌ npm start๋ฅผ ํจ.
์ด ๋ ๊ฐ์์์๋ macํ๊ฒฝ์์ simulator๋ฅผ ์ด์ฉํ์ง๋ง, windowํ๊ฒฝ์ด๊ธฐ ๋๋ฌธ์ ํด๋ํฐ์ผ๋ก ์คํ.
ํ์ฅํ๋ก๊ทธ๋จ์ด๋ window ์๋ฎฌ๋ ์ดํฐ ๊ฐ์๊ฒ ์๋ ์ฐพ์๋ณด๋ค๊ฐ ํด๋ํฐ์์ ์คํ ํ์๋คํ์ฌ, ํด๋ํฐ์ผ๋ก ์คํ.
์ฝ์์ฐฝ ํ์ธ์ ์ํด ์๋ ๋งํฌ๋ฅผ ํตํด ํด๋ํฐ์์ ์คํํ ๊ฒ์ ๋ธ๋ผ์ฐ์ ์์ ํ์ธํจ.
https://backstreet-programmer.tistory.com/30
ํด๋ํฐ ์ ์ ์ external ๋งํฌ๋ก ์ ์!
const arrow = document.querySelector('.arrow');
const speed = document.querySelector('.speed-value');
arrow์ speed์ ์์๋ค์ ์ ํ.
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!!!');
});
์ฝ์์ ๋์ด์ค๋ ๋ฐ์ดํฐ๋ฅผ ์ฐ์ด๋ณด๋ฉด,
์ด ์ฌ์ง์ heading๊ณผ speed๋ถ๋ถ์ด ์๋์ ๋ฐฉํฅ์ด ๋๋ค.
(์ฒ์์ ๊ทธ๋ฅ ์๋ฎฌ๋ ์ดํฐ ๋ฐฉ๋ฒ์ ์ฐพ๋ค๊ฐ ์ธํฐ๋ท์ผ๋ก ์ผ์ ์ด๋ค ๋ฐ์ดํฐ๊ฐ ๋์ด์ค๋์ง๋ง ํ์ธํด์ null์ด ์ถ๋ ฅ๋จ.)
data์ coords์์ ์ ๋ณด๋ค์ด ๋ด๊ฒจ ์๋ค.
๊ฐ์์์๋ ๊ฐ์ด ์๋์ค์ง๋ง, ๋ด ํฐ์ผ๋ก ์คํ ํ์ ๋ 0.xxxxxx๊ฐ์ด ๋์ *100์ ํ์ฌ ์ผ๋จ ๊ฐ์ด ํ์๋๋ ์ง ํ์ธ.
๋ฐฑํฑ(``)์ ์ด์ฉํ์ฌ ๋์ด ์จ ๊ฐ๋ค์ ์ด์ฉํด ์๋๋ฅผ ์ถ๋ ฅํ๊ณ , heading๋งํผ rotate๋ฅผ ์ด์ฉํด ๋๋ฆฐ๋ค.