본문 바로가기
Web/JavaScript

[JavaScript] 객체 - 8️⃣ navigator 객체 & geolocation

by coding-choonsik 2023. 4. 12.
728x90
반응형
SMALL

1. navigator 객체

  • 브라우저 공급자 및 버전 정보 등을 포함한 브라우저에 대한 정보를 저장하는 객체

        

2. geolocation

  • GPS 정보를 수신하는 프로퍼티

<!DOCTYPE html>
<html lang="en">
<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>navigator</title>
</head>
<body>
    <h2>navigator</h2>
    <script>
        const success = function(loc){
            console.log(loc.coords.latitude)   //위도
            console.log(loc.coords.longitude)  // 경도
        }
        const fail = function(msg){
            console.log(msg.code);  // 해당 메시지를 찍음
        }
        navigator.geolocation.getCurrentPosition(success, fail);   // (성공했을 때 변수or함수, 실패했을 때 변수or함수)
    </script>
</body>
</html>

▲ 현재 브라우저를 접속한 위치의 위도와 경도


MDN

https://developer.mozilla.org/ko/docs/Web/API/Navigator

 

Navigator - Web API | MDN

Navigator 인터페이스는 사용자 에이전트의 상태와 신원 정보를 나타내며, 스크립트로 해당 정보를 질의할 때와 애플리케이션을 특정 활동에 등록할 때 사용합니다.

developer.mozilla.org

 

 

728x90
반응형
LIST