2012-08-28 3 views
0

다음과 같이 호출되는 자바 스크립트가있는 간단한 웹 사이트를 만들었습니다.getCurrentPosition이 실패하게되는 이유는 무엇입니까?

navigator.geolocation.getCurrentPosition (show_map, show_map_error);

나는 웹 사이트를 인터넷에 연결했습니다. 다른 위치에있는 다른 PC (GPS 가젯 없음)에서 웹 사이트를 열려고했습니다. 집에서 하나, 친구 사무실에서 하나.

그러나 스크립트가 항상 위치를 얻는 것은 아닙니다. 무엇이 문제입니까?

감사합니다.

답변

0

GPS가 연결되어 있지 않은 경우 위치를 반환 할 수있는 방법이 보장되지 않습니다.

캐시 된 위치를 대신 사용해 볼 수 있습니다. API 사양에서 다음을 참조하십시오.

// Request a position. We only accept cached positions, no matter what 
// their age is. If the user agent does not have a cached position at 
// all, it will immediately invoke the error callback. 
navigator.geolocation.getCurrentPosition(successCallback, 
             errorCallback, 
             {maximumAge:Infinity, timeout:0}); 

function successCallback(position) { 
    // By setting the 'maximumAge' to Infinity, the position 
    // object is guaranteed to be a cached one. 
    // By using a 'timeout' of 0 milliseconds, if there is 
    // no cached position available at all, the user agent 
    // will immediately invoke the error callback with code 
    // TIMEOUT and will not initiate a new position 
    // acquisition process. 
    if (position.timestamp < freshness_threshold && 
     position.coords.accuracy < accuracy_threshold) { 
    // The position is relatively fresh and accurate. 
    } else { 
    // The position is quite old and/or inaccurate. 
    } 
} 

function errorCallback(error) { 
    switch(error.code) { 
    case error.TIMEOUT: 
     // Quick fallback when no cached position exists at all. 
     doFallback(); 
     // Acquire a new position object. 
     navigator.geolocation.getCurrentPosition(successCallback, errorCallback); 
     break; 
    case ... // treat the other error cases. 
    }; 
} 

function doFallback() { 
    // No cached position available at all. 
    // Fallback to a default position. 
} 
+0

PC에 GPS가 연결되어 있지 않은데 왜 위치를 얻을 수 있습니까? – fasisi

+0

그리고 GPS가 내장 된 블랙 베리 전화가 있습니다. 휴대 전화의지도 응용 프로그램은 GPS에서 위치 정보를 가져올 수 있습니다. 그러나 웹 사이트는 입장을 얻을 수 없습니다. 문제가 무엇입니까? – fasisi

+0

사양에 따르면 장치는 IP 주소를 포함하여 위치를 대략적으로 계산하는 여러 가지 방법을 사용할 수 있습니다. –

관련 문제