2013-08-23 4 views
0
function getMyLocation() { 
if(navigator.geolocation){ 
    navigator.geolocation.getCurrentPosition(displayLocation); 
} 
else{ 
    alert("Geolocation not supported"); 
} 
} 

var user_lat; 
var user_long; 
var d_long; 
var d_lat; 

function displayLocation(position) { 
user_lat = position.coords.latitude; 
user_long = position.coords.longitude; 
alert("you are at lat: " + user_lat + " ,longitude: " + user_long); 
} 

function getLocationDestination() { 
var e = document.getElementById("building"); 
var strUser = e.options[e.selectedIndex].text; 

alert("you want to go to " + strUser + " and the cords are " + building_array[strUser]); 

getMyLocation(); 
alert("heelo"); 
displayLocation(position); 
alert("heelo"); 
displayLocation(position); 
} 

코드는 매우 간단하지만 geoLocationDestination()을 실행할 때 displayLocation (위치)에서 두 번째 경고를받지 못합니다. while 루프에서는 20 번 실행하지 않습니다. 두 번째 안녕하세요 후 프로그램이 종료됩니까? 어떤 아이디어? 당신이 별도로 위치 매개 변수를 전달이 getMyLocation에서 기능하지 호출 할 필요가 있기 때문에 내가 당신의 코드를 실행하면자바 스크립트 HTML5 Geolocation 버그?

+0

getLocationDestination()에서'position'이 정의되지 않은 것 같습니다 ... ... – Passerby

답변

0

, 나는 위치가 displaylocation 함수에서 정의되지 얻을,이, 아래 는 작업 코드

<html> 
<script> 
function getMyLocation() { 
if(navigator.geolocation){ 
navigator.geolocation.getCurrentPosition(displayLocation); 
} 
else{ 
alert("Geolocation not supported"); 
} 
} 

var user_lat; 
var user_long; 
var d_long; 
var d_lat; 

function displayLocation(position) { 


user_lat = position.coords.latitude; 
user_long = position.coords.longitude; 
alert("you are at lat: " + user_lat + " ,longitude: " + user_long); 
} 

function getLocationDestination() { 
var sel = document.getElementById("foo"); 
var option = sel.options[sel.selectedIndex]; 
var text = option.text; 
var value = option.value; 
alert(text, value); 

getMyLocation(); 

} 

</script> 
<input type="submit" id=hello onclick=getLocationDestination();> 
<select id="foo"> 
<option value="1">One</option> 
<option value="2">Two</option> 
<option value="3">Three</option> 
</select> 

</html> 

이 코드는 선택 상자에서 선택된 값을 표시하고 경고를 표시합니다 (코딩 부분에 "가고 싶다"라고 가정합니다. HTML 부분을 포함하지 않았으므로 확신 할 수 없음) 은 위도와 경도를 경고합니다