JAVASCRIPT를 이용한 GPS API(Geolocation)

JMON·2021년 1월 14일
0

JavaScript

목록 보기
2/2
post-thumbnail

📄 설명

📣 기본

페이지 접속자의 위치를 확인 할 수 있는 기능

📄 사용방법

📣 기본 코드

<script>
function getLocation() {
  if (navigator.geolocation) { // GPS를 지원하면
    navigator.geolocation.getCurrentPosition(function(position) {
      alert(position.coords.latitude + ' ' + position.coords.longitude);
    }, function(error) {
      console.error(error);
    }, {
      enableHighAccuracy: false,
      maximumAge: 0,
      timeout: Infinity
    });
  } else {
    alert('GPS를 지원하지 않습니다');
  }
}
getLocation();
</script>

📣 위치 변경시 콜백

var watchId = navigator.geolocation.watchPosition(function(position) {
  console.log(position.coords);
});

📄 Response

accuracy: __
altitude: null
altitudeAccuracy: null
heading: null
latitude: __._____
longitude: ___._______
speed: null

📄 참고자료

🎈 Document : https://developer.mozilla.org/ko/docs/WebAPI/Using_geolocation

profile
Fullstack Developer

0개의 댓글