2015-02-04 3 views
0

geolocation HTML5를 사용하여 사용자의 위치를 ​​얻으려고합니다. PC에서 좋은 작품이지만, 내 모든 모바일 장치 (삼성 메모, 삼성 갤럭시 S4 및 아이폰 6)에서 작동하지 않는 오류 개체를 표시하지 않습니다. 내가 말했듯이, 그것이 작동하지geolocation이 모바일에서 작동하지 않습니다.

function showPosition(position) { 
    var coor = position.coords.longitude+", "+position.coords.latitude; 
    alert(coor); 
} 
function errorPosition(error) { 
    alert(error); 
} 
function toggleGeolocation() { 
    navigator.geolocation.watchPosition(showPosition,errorPosition); 
} 

과도 표시되지 않는 오류 :

이 내 코드입니다. Geolocation에 대한 승인을 요청하고 허용을 클릭하면 GPS가 작동하며 문제는 무엇입니까 ?? 모든 기기에서 Google 크롬을 사용하고 있습니다.

+0

사파리와 크롬에서 iOS 8.1을 실행하는 iPhone 5와 Chrome의 Galaxy Tab 4 Kit-Kat 및 Android 주식 브라우저에서 작동합니다. 이 코드는이 문제의 원인이 아닙니다. – blex

답변

2

이 코드를 사용해도 올바르게 동작합니다.

var successHandler = function(position) { 
alert(position.coords.latitude); 
alert(position.coords.longitude); 
}; 

var errorHandler = function (errorObj) { 
alert(errorObj.code + ": " + errorObj.message); 

alert("something wrong take this lat " + "26.0546106); 
alert("something wrong take this lng " +-98.3939791); 

}; 

navigator.geolocation.getCurrentPosition( 
successHandler, errorHandler, 
{enableHighAccuracy: true, maximumAge: 10000}); 
+0

대답을 주셔서 감사합니다,하지만 난 여전히 캔트 가져 오기 오류 MSG, 내가 추가 한이 같은 경고 메시지 : 기능 initializeGeo() { 경우 (네비게이터 && navigator.geolocation) { navigator.geolocation.getCurrentPosition (showPosition, errorCallback); 경고 ("OK"); } else { 경고 ('Geolocation is not supported'); } } 이 메시지를 받으면 Geolocation은 지원되지만 행은 다음과 같습니다. navigator.geolocation.getCurrentPosition (showPosition, errorCallback); 아무 것도하지 않습니다 ... – Jordan

+0

sup 조던 내가 코멘트에서 모든 코드를 읽을 수 있습니다 – Ethaan

+0

getCurrentPosition을 호출하는 행 다음에 경고 메시지를 추가했습니다. 경고 메시지를 볼 수는 있지만 getCurrentPosition의 결과는 볼 수 없습니다. – Jordan

2

네비게이터는 https 웹 사이트의 Android에서만 작동합니다. 다음은 HTTP를 사용하는 경우 오류가 표시되지 않습니다 예입니다,하지만 난 내가 무슨 짓을했는지 같은 직면 한

<!DOCTYPE html> 
<html> 
<body> 

<p>Click the button to get your coordinates.</p> 

<button onclick="getLocation()">Try It</button> 

<p id="demo"></p> 

<script> 
var x = document.getElementById("demo"); 

function getLocation() { 
    if (navigator.geolocation) { 
     navigator.geolocation.getCurrentPosition(showPosition); 
    } else { 
     x.innerHTML = "Geolocation is not supported by this browser."; 
    } 
} 

function showPosition(position) { 
    x.innerHTML = "Latitude: " + position.coords.latitude + 
    "<br>Longitude: " + position.coords.longitude; 
} 
</script> 

</body> 
</html> 
0

안녕하세요 @jordan이

먼저 메이크업입니다 (https://www.w3schools.com/HTML/tryit.asp?filename=tryhtml5_geolocation에서) HTTPS에 잘 작동합니다 기기에서 Chrome 브라우저를 업데이트했는지 확인하십시오.

1))

2 모바일 장치 1

의 위치 서비스를 시작 그 권한에 대해 나에게 물었다 이렇게함으로써 대해서 getLocation() 함수는 onLoad에게

에게 전화를 잘했다.

희망이 있습니다.

관련 문제