function initMap() { 


var map = new google.maps.Map(document.getElementById('map'), {

zoom: 14

});

  

navigator.geolocation.getCurrentPosition(function(position) {

var pos = {

lat: position.coords.latitude,

lng: position.coords.longitude

};

map.setCenter(pos);

});

}

  

<script>

navigator.geolocation.watchPosition(function(position) {

    alert("위도 : "+position.coords.latitude); 

    alert("경도 : "+position.coords.longitude);

});

</script>


navigator.geolocation.getCurrentPosition으로 지금 현재 위치를 반환하고 그 위치를 구글맵에 이용할 수 있다. 위 코드 참조. 


다만 모바일 및 데스크톱용 크롬 최신 버전에서는 해당 자료를 웹에 올렸을 때 https를 적용해야 해당 기능을 이용할 수 있다. 익스플로러 및 엣지에서는 아직 이용할 수 있다. 


htm 파일을 만들어 스마트폰에 복사를 해서 크롬에서 불러오면 이 기능을 이용할 수 있다. 다만 삼성 브라우저에서는 내용을 읽을 수 없으며 외부 SD카드에 파일을 저장했을 때에도 읽을 수 없다.


+ Recent posts