2013-02-26 4 views
-1

javascript를 사용하여 geolocation을 사용하는 방법을 배우려고합니다. 그러나 스크립트는 내가 생각한대로 작동하지 않습니다.내 코드에서 Geolocation이 작동하지 않습니다.

<!doctype html> 
<html> 
<title> Testing Geolocation</title> 
<head> 
    <script> 
    function displayLocation(){ 
    var latitude = position.coords.latitude; 
    var longitude = position.coords.longitude; 
    var div = document.getElementById("myLocation"); 
    div.innerHTML =" You are at Latitude "+latitude+" and longitude "+longitude);}   
    window.onload = getMyLocation;} 
    function getMyLocation{ 
    if(navigator.geolocation){ 
    navigator.geolocation.getCurrentPosition(displayLocation);} 
    else{ 
    alert("Oops! Geolocation function not supported");} 

</script> 
</head> 
<body> 
<div id="myLocation"> 
Your location will go here. 
</div> 
</body> 
</html> 

안녕, 오타를 수정 한 후, 스크립트가 여전히 작동하지 않습니다 : 다음은 내 코드입니다. 위도와 경도가 표시되지 않습니다.

+0

내 생각을 예외없이'navigator.gelocation' 철자가 잘못되어'navigator.geolocation'이어야합니다. –

+0

또한이 포스트가 설명하는 것처럼 http://stackoverflow.com/questions/3397585/navigator-geolocation-getcurrentposition-sometimes-works-sometimes-doesnt에서'getMyLocation()'전에'displayLocation' 함수를 정의하십시오. –

+0

안녕하세요, 오타를 수정 한 후 스크립트가 여전히 작동하지 않습니다. 위도와 경도가 표시되지 않습니다. – user2106416

답변

1

displayLocation 함수에 인수 'position'을 추가하십시오.

UPD : 일부 오타가 있습니다. 오류 추적을 위해 Chrome/FF 콘솔을 사용합니까?

<script> 
function displayLocation(position){ 
    var latitude = position.coords.latitude; 
    var longitude = position.coords.longitude; 
    var div = document.getElementById('myLocation'); 
    div.innerHTML = "You are at Latitude "+latitude+" and longitude "+longitude; 
}   

function getMyLocation(){ 
    if(navigator.geolocation){ 
     navigator.geolocation.getCurrentPosition(displayLocation); 
    } else { 
     alert('Oops! Geolocation function not supported'); 
    } 
} 

getMyLocation(); 
</script> 
+0

여전히 작동하지 않습니다. :( – user2106416

0
당신의 getMyLocation 기능 당신이 누락

(), 그 기능에 대한 최종 }을 (당신은 창로드 라인 이후 추가 }이) :이 시도

<!doctype html> 
<html> 
<title> Testing Geolocation</title> 
<head> 
<script> 
    function displayLocation(position){ 
     var latitude = position.coords.latitude; 
     var longitude = position.coords.longitude; 
     var div = document.getElementById("myLocation"); 
     div.innerHTML =" You are at Latitude "+latitude+" and longitude "+longitude;}   

    function getMyLocation(){ 
     if(navigator.geolocation){ 
      navigator.geolocation.getCurrentPosition(displayLocation);} 
     else{ 
      alert("Oops! Geolocation function not supported");} 
    } 

window.onload = getMyLocation; 
</script> 
</head> 
<body> 
<div id="myLocation"> 
Your location will go here. 
</div> 
</body> 
</html> 
관련 문제