PhoneGap-2.9.0 navigator.geolocation.getCurrentPosition(onSuccess,onError);不工作

时间:2014-08-05 11:08:47

标签: android cordova

<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8">

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

    function onDeviceReady() { alert('onDeviceReady Function works');
        var option = {frequency:500,maximumAge: 0, timeout: 1000, enableHighAccuracy:true};
        navigator.geolocation.getCurrentPosition(onSuccess, onError, option );
    }

    function onSuccess(position) { alert("It Works!!!");
        var element = document.getElementById('geolocation');
        element.innerHTML = 'Latitude: '  + position.coords.latitude  + '<br />' +
                            'Longitude: ' + position.coords.longitude;
    }

    function onError(error) {
         //alert(error);
        alert('code: '    + error.code    + '\n' + 'message: ' + error.message + '\n');
    }

 </script>


<p id="geolocation">Finding geolocation...</p>

上面的代码deviceready()功能正常,但alert("It Works!!!");无效。请告诉我为什么它在我的设备模拟器中不起作用?

我收到错误消息。

截图:

enter image description here

1 个答案:

答案 0 :(得分:0)

超时增加到&#39; 5000&#39;或者&#39; 10000&#39;并且还增加 maximumAge

 { maximumAge: 3000, timeout: 5000, enableHighAccuracy: true };

检查PhoneGap Error documentation