2014-10-22 2 views
0

HTML5 및 Phonegap을 사용하여 일부 앱 개발을 시작했습니다. lon/lat 좌표를 표시하기 위해 Wi-Fi가 비활성화 된 상태에서 GPS를 사용하려고합니다. 코드가 작동합니다 ... 일종의.Android Nexus 7 GPS 센서 사용

앱을 여는 경우 위치를 알 수 없습니다. Google지도 또는 GPS 테스터 인 Geolocation을 사용하는 다른 앱을 연 다음 내 앱으로 다시 전환하면 좌표가 작동하고 업데이트됩니다. 내 앱이 GPS 센서를 켜지 않는 것과 같지만 일단 켜면 내 앱에서 GPS 센서에 액세스 할 수 있습니다.

내가 누락 된 항목이 있습니까?

코드;

<script> 
    window.onload = function() { 
    var startPos; 

    if (navigator.geolocation) { 
     navigator.geolocation.getCurrentPosition(function(position) { 
     enableHighAccuracy: true; 
     startPos = position; 
     document.getElementById("startLat").innerHTML = startPos.coords.latitude; 
     document.getElementById("startLon").innerHTML = startPos.coords.longitude; 
     }, function(error) { 
     alert("Error occurred. Error code: " + error.code); 
     // error.code can be: 
     // 0: unknown error 
     // 1: permission denied 
     // 2: position unavailable (error response from locaton provider) 
     // 3: timed out 
     }); 

     navigator.geolocation.watchPosition(function(position) { 
     enableHighAccuracy: true; 
     document.getElementById("currentLat").innerHTML = position.coords.latitude; 
     document.getElementById("currentLon").innerHTML = position.coords.longitude; 
     document.getElementById("distance").innerHTML = 
      calculateDistance(startPos.coords.latitude, startPos.coords.longitude, 
          position.coords.latitude, position.coords.longitude); 
     }); 
    } 
    }; 

    // Reused code - copyright Moveable Type Scripts - retrieved May 4, 2010. 
    // http://www.movable-type.co.uk/scripts/latlong.html 
    // Under Creative Commons License http://creativecommons.org/licenses/by/3.0/ 
    function calculateDistance(lat1, lon1, lat2, lon2) { 
    var R = 6371; // km 
    var dLat = (lat2-51.202399).toRad(); 
    var dLon = (lon2-parseFloat(-1.479645)).toRad(); 
    var a = Math.sin(dLat/2) * Math.sin(dLat/2) + 
      Math.cos(51.202399.toRad()) * Math.cos(lat2.toRad()) * 
      Math.sin(dLon/2) * Math.sin(dLon/2); 
    var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
    var d = R * c; 
    return d.toFixed(2); 
    } 
    Number.prototype.toRad = function() { 
    return this * Math.PI/180; 
    } 
</script> 

안부 앤디

답변

0

사용 org.apache.cordova.geolocation 플러그인.

https://github.com/apache/cordova-plugin-geolocation

+0

고마워요! 내 무지를 용서하지만 이것은 나에게 매우 새로운 일이다. Dreamweaver를 사용하여 코드를 편집하고 있습니다. 그런 다음 파일을 압축하여 build.phonegap.com에 업로드하고 내 기기에 앱을 설치합니다. 이 플러그인이 어떻게 적용되는지, 또는 어디서 이해할 수 있습니까? 올바른 방향으로 나를 가리킬 수 있습니까? – AJUK

+0

아, PhoneGap Build를 사용합니다. 이 페이지 http://docs.build.phonegap.com/en_US/configuring_plugins.md.html#Plugins 및 https://build.phonegap.com/plugins/1176을 읽으십시오. – wf9a5m75