안드로이드 플랫폼에 익숙하지 않으므로 기존 샘플 코드로 학습을 시작하고 싶었습니다. 나는 GPS 좌표를 획득하려고 노력 중이므로 아래 코드를 보았습니다. 아래 코드에서 장치가 시작되면 onDeviceReady() 메서드는 실행 중이지만 onSuccess() 메서드는 실행되지 않습니다. 이 문제를 해결하는 방법? 나는 내 프로젝트에서 cordova-2.8.0.jar를 사용하고있다.Android Phonegap의 위도 경도 추적?
<!DOCTYPE html>
<html xml:lang="en" lang="en">
<head>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no;">
<title>PhoneGap</title>
<script type="text/javascript" charset="utf-8">
// Wait for Cordova to load
//
document.addEventListener("deviceready", onDeviceReady, false);
var watchID = null;
// Cordova is ready
//
function onDeviceReady() {
// Throw an error if no update is received every 30 seconds
alert("Test1");
var options = {
timeout: 3000
};
alert("TEst2"+options);
watchID = navigator.geolocation.watchPosition(onSuccess, onError, options);
alert("TEst3");
}
// onSuccess Geolocation
//
function onSuccess(position) {
alert("RAmesh");
/* var element = document.getElementById('geolocation');
element.innerHTML = 'Latitude: ' + position.coords.latitude + '<br />' +
'Longitude: ' + position.coords.longitude + '<br />' +
'<hr />' + element.innerHTML;*/
}
// onError Callback receives a PositionError object
//
function onError(error) {
/* alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');*/
}
</script>
</head>
<body>
<p id="geolocation">Finding geolocation...</p>
</body>
</html>
코드를 조금 재정렬했는데 [this] (http://pastebin.com/GTjaQ92i)를 시도하십시오. 귀하의 watchPosition에서 당신은 onSuccess와 onError를 호출하고 있지만 괄호를 사용하지 않으면 그 이름을 가진 변수를 찾고 있습니다. 그런 식으로 호출하려는 경우 해당 변수 이름에 함수를 할당해야합니다. –