2014-03-18 4 views
0

이 자습서를 온라인으로 진행하면서 HTML5 및 Google지도 API의 새로운 기능을 사용하여 현재 위치 (Geolocation)를 얻는 방법을 설명합니다.navigator.Geolocation GetCurrentpositon이 Chrome 및 Firefox에서 작동하지 않습니다.

˚<!DOCTYPE html> 
<html> 

<head> 
<meta charset="UTF-8"> 
<title>Navigator</title> 
</head> 
<script src="js/jquery.js"></script> 
<script src="http://maps.google.com/maps/api/js?sensor=true"></script> 

<script> 

x = navigator.geolocation; 

x.getCurrentPosition(success, failure); 

function success(position) { 
    var mylat = position.coords.latitude; 
    var mylong = position.coords.longitude; 
    $('#lat').htm(mylat); 
    $('#long').html(mylong); 
} 

function failure() { 
    $('#lat').html("<h3> No co-ordinates available!</h3>"); 
} 
</script> 

<body> 
<!--map placeholder --> 
<div id="map"> 

</div> 

<div id="lat"></div> 
<div id="long"></div> 
</body> 



</html> 

Google과 Firefox 모두에서이 코드를 실행하려고했습니다. Google에서 내 현재 위치를 볼 수 없도록 차단합니다. 실패 메시지가 나타납니다. "사용할 수있는 좌표가 없습니다!" 파이어 폭스는 너무 잘 작동합니다!

Chrome의 문제점은 무엇입니까? 친구가 내 IP 주소를 사용하여 내 위치를 가져와야한다고 제안했습니다. 무엇을해야합니까? 내 현재 위치를 차단하는 Chrome이있는 방법이 있습니까? . '=>'$ (';

+0

당신은 오타'$ ('# 위도 ') HTM (mylat)가 IE/크롬/FF 모바일 브라우저 거의 모든 작품 사용 #lat ') .html (mylat);' – Mathias

+0

Chrome에서 작동합니다. http://jsfiddle.net/borglinm/LVBVy/ – Mathias

+0

먼저 크롬 설정에서 위치 설정을 확인합니다. (개인 정보 -> 콘텐츠 설정 아래에 있음)에서 허용 또는 위치 확인을 요청하도록 설정하십시오. 이미 한 번 차단했기 때문에 '예외 관리'에서 예외를 제거 할 수도 있습니다. –

답변

1

이 코드는

if (navigator.geolocation) { 
    var latitude = null; 
    var longitude = null; 
    navigator.geolocation.getCurrentPosition(function (position) { 
     latitude = position.coords.latitude; 
     longitude = position.coords.longitude; 
    }); 

} else { 
    alert("Geolocation API is not supported in your browser"); 
}; 
관련 문제