티스토리 뷰
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
- Geolocation
- useState
- Hook
- getitem
- e.preventDefault()
- padStart
- 데드락
- Let
- 교착상태
- innerText
- removeitem
- console.log
- setTimeout
- createElement
- var
- 호이스팅
- new Date()
- classList
- 깊은복사
- getCurrentPosition
- Navigator
- const
- Return
- 스코프
- setitem
- 얕은복사
- setinterval
- 브라우저 저장소
- localStorage
- react
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
글 보관함
