Phonegap navigator.geolocation.getCurrentPosition出错

时间:2013-01-15 08:11:02

标签: android cordova

当我尝试在HTML页面中获取我的地理位置时,我收到以下错误: 错误代码:2 错误消息:最后一个位置提供程序不再可用 这是我的代码:     

// onSuccess Callback
//  This method accepts a `Position` object, which contains
//  the current GPS coordinates
//
var onSuccess = function(position) {
   };

var HospitalsRecord = [];



//onError Callback receives a PositionError object
//
function onError(error) {
   alert('code: '    + error.code    + '\n' +
         'message: ' + error.message + '\n');
}

// Cordova is ready
//
function onDeviceReady() {
    navigator.geolocation.getCurrentPosition(onSuccess, onError, { maximumAge: Infinity, timeout: 15000, enableHighAccuracy: true });

}

// Wait for Cordova to load
//
document.addEventListener("deviceready", onDeviceReady, false);


</script>

为什么会这样?

提前致谢

了Kobi

2 个答案:

答案 0 :(得分:2)

尝试将此添加到config.xml中,该文件位于应用程序的根目录中:

<gap:plugin name="org.apache.cordova.geolocation" />

这可以添加到小部件标记内的任何位置。这对我有帮助。

答案 1 :(得分:-1)

试试这样..

<script type="text/javascript">

        // Wait for Cordova to load 

        document.addEventListener("deviceready", onDeviceReady, false);

        // Cordova is ready      

        function onDeviceReady() {

            navigator.geolocation.getCurrentPosition(onSuccess, onError);

        }

        // onSuccess Geolocation      

        function onSuccess(position) {

            var element = document.getElementById('geolocation');

            element.innerHTML = 'Latitude: ' + position.coords.latitude + '<br />' +

                            'Longitude: ' + position.coords.longitude + '<br />' +

                            'Altitude: ' + position.coords.altitude + '<br />' +

                            'Accuracy: ' + position.coords.accuracy + '<br />' +

                            'Altitude Accuracy: ' + position.coords.altitudeAccuracy + '<br />' +

                            'Heading: ' + position.coords.heading + '<br />' +

                            'Speed: ' + position.coords.speed + '<br />' +

                            'Timestamp: ' + position.timestamp + '<br />';

        }

        // onError Callback receives a PositionError object     

        function onError(error) {

            alert('code: ' + error.code + '\n' +

              'message: ' + error.message + '\n');

        }

    </script>

了解更多信息..

try this