Develope/SMART WEB

Geolocation API ▣ 아이폰웹 개발

친절한 웬디양~ㅎㅎ 2011. 4. 25. 15:57
반응형

 

* Geolocation API 란?

  - Geolocation API 는 웹페이지에서 현재 자기 위치를 얻기위한  JavaScript API

 

* 지원현황

  - Firefox(V3.5이상), Chrome(V5.0이상), Safari(V5.0이상), Opeara(V10.60 이상)

 

* 위치정보 확인

  - 지원 가능한 브라우져 확인

     <script>

      function initGeolocation()
     {
        if( navigator.geolocation )
        {
          alert("Geolocation API를 지원함");
        }else{
          alert("Geolocation API 를 지원하지 안습니다.");
        }
     }

    </script>

위 소스를 확인해보면 geolocation API를 사용할 수 있는 브라우져인지 확인 가능하다.

 

* 위치정보 확인

 - 현재 접속한 위치의 좌표값 알아보기

    <script type="text/javascript">
        navigator.geolocation.getCurrentPosition(handler);
        function handler(location)
       {
         var message = document.getElementById("message")
         message.innerHTML += "<P>Lon : "+location.coords.longitude + "<br> 

                                        Lat : "+location.coords.latitude + "</P>";  

         lat =  location.coords.latitude;
         lon =  location.coords.longitude;
       }
    </script> 

    <div id="message"></div>

    위에 소스를 적용하며 현재 좌표값을 얻을 수 있다.

   (참고로 좌표값은 위경도 값이다.)

   ( TIP : 아이폰에서 위치값이 나오지 않는 사람들은 꼭 확이해볼것

     1. 설정 -> 일반 -> 위치 서비스 -> safari 가 켜져있는지 확인

     2.  설정 -> safari -> 위치경고 켜있는지 확인 , javascript 켜있는지 확인

     위에 두개만 다 되어있다면 페이지 만들어서 다시 확인)

 

  - 위치정보를 계속 확인

     현재 위에서는 getCurrentPosition 사용하여 좌표값을 가져왔지만

     watchPosition 함수를 이용하면 연속적인 좌표값을 얻어올수 있다.

 

 - 위치 정보 확인

     clearWatch 를 사용하면 위치정보 확인을 종료한다.

 

  - 연결 확인 함수 

      position.timestamp; //정보 얻은 시간 (ms)
      position.coords.latitude; //위도
      position.coords.longitude; //경도
      position.coords.accuracy; //위도,경도의 오차 (m)
      position.coords.altitude; //고도  (null)
      position.coords.altitudeAccuracy;//고도의 오차 (m)
      position.coords.speed; 진행속도 (m/s)
      position.coords.heading; 정북의 시계방향의 각도 (null)

[출처] Geolocation API|작성자 동혀니

반응형