티스토리 뷰
1. navigator 함수를 이용해 사용자의 위치를 알아내는 코드 작성.
navigator.geolocation.getCurrentPosition ( ) 라는 코드를 작성해준다.
navigator.geolocation을 부르면 브라우저에서 자동으로 위치 좌표를 준다.
이때 getCurrentPosition 은 2개의 argument가 필요하다. 앞쪽에는 모든 게 잘 됐을 때 실행될 함수인 onGeoOk 함수를, 뒤에는 실패했을 때 실행될 함수인 onGeoError 함수를 입력한다.
2.onGeoError함수가 실행될 때
에러가 났다는 것을 사용자에게 알려주기 위해서
alert("Can't find you. No weather for you."); 를 해준다.
3. onGeoOk 함수가 실행될 때
function onGeoOk(position){
const lat = position.coords.latitude;
const lng = position.coords.longitude;
console.log("You live in", lat, lng);
}
자바스크립트가 position으로 user의 위치를 전달해준다.
position은 object 이고 위도와 경도 값이 들어있다. positon.coords.latitude와
position.coords.longitude 를 변수에 저장하고 console.log를 해서 사용자에게 보여준다.
'개발공부 > JS' 카테고리의 다른 글
[JS] localStorage에 배열로 값 쌓기 (0) | 2023.06.25 |
---|---|
[JS] 랜덤으로 명언, 배경사진 불러오기 (0) | 2023.06.23 |
[JS] 현재시간 불러오기 (0) | 2023.06.22 |
[JS] localstorage에 item 생성, 삭제, 조회 (0) | 2023.06.21 |
[JS] e.preventDefault() 란?? (0) | 2023.06.21 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- react
- removeitem
- classList
- Navigator
- 깊은복사
- localStorage
- getitem
- 데드락
- const
- 얕은복사
- 스코프
- innerText
- setinterval
- e.preventDefault()
- var
- padStart
- new Date()
- useState
- 교착상태
- 호이스팅
- console.log
- Hook
- setitem
- createElement
- 브라우저 저장소
- Geolocation
- getCurrentPosition
- setTimeout
- Return
- Let
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
글 보관함