2012-08-24 5 views
0


함수에서 고도 값을 가져 오는 데 몇 가지 문제가 있습니다. 나는 그 가치를 얻을 수 없다. 누군가가 그 해결책을 가지고 있다면 좋을 것입니다.
다음 내 코드입니다 :고도 요청 값을 되 찾는 방법

function getElevation(latLng) { 
    seaLevel = 'Error'; 
    var locations = []; 
    locations.push(latLng); 
    //Create a LocationElevationRequest object using the array[0] value 
    var positionalRequest = { 
    'locations': locations 
    } 
    //Initiate the location request 
    elevationService.getElevationForLocations(positionalRequest, function(results, status) { 
    if (status == google.maps.ElevationStatus.OK) { 
     // Retrieve the first result 
     if (results[0]) { 
     var seaLvl = parseFloat(results[0].elevation.toFixed(1)); 
     } 
     dropElevation(seaLvl); 
    } 
     document.getElementById("response").innerHTML = seaLevel; 
    }); 
    function dropElevation(tmpLevel) { 
    //alert(tmpLevel); at this point the value is correct 
    seaLevel = tmpLevel; 
    } 
    return seaLevel; //at this point the value is always as defined above 
} //End function (getElevation) 

그것의 호출은 다음과 같습니다 : 내가 잘못
귀도

답변

0

표고를하고 있어요 걸 보여주기 위해 사전에

감사합니다 서비스는 비동기식입니다. 콜백 루틴 내에서 값을 사용해야합니다. 이 같은

elevationService.getElevationForLocations(positionalRequest, function(results, status) 
{ 
    if (status == google.maps.ElevationStatus.OK) { 
    // Retrieve the first result 
    if (results[0]) { 
     var seaLvl = parseFloat(results[0].elevation.toFixed(1)); 
     dropElevation(seaLvl); 
     document.getElementById("response").innerHTML = seaLevel; 
    } else { 
     alert("no result"); 
    } 
    } 
}); 

그리고 뭔가 : 이런 식으로 뭔가 (메시지가 서버에서 반환 될 때까지의 값을 사용할 수없는)

이 작동하지 않습니다.

+0

답장을 보내 주셔서 감사합니다 ... –

관련 문제