我为项目创建了cordova项目,我添加了android平台和地理位置插件,即
cordova-plugin-geolocation 2.0.0“Geolocation”
当我执行代码时,我收到了错误
@timestamp
这是代码
"Error in Success callbackId: Geolocation876750358 : TypeError: Cannot read property '0' of undefined", source: file:///android_asset/www/cordova.js (312)
答案 0 :(得分:0)
我认为,插件安装不正确。以这种方式测试:
<!DOCTYPE html>
<html>
<head>
<title>Device Properties Example</title>
<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() {
if (!navigator.geolocation) {
alert("Error: Plugin not working!");
} else {
navigator.geolocation.getCurrentPosition(onSuccess, onError);
}
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 />';
}
function onError(error) {
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
}
}
</script>
</head>
<body>
<p id="geolocation">Finding geolocation...</p>
</body>
</html>
如果您看到错误提醒,则应按照文档https://www.npmjs.com/package/cordova-plugin-geolocation重新卸载并安装插件。
<强>更新强>
抱歉,我还没有看到,成功和错误功能必须在设备就绪事件功能中。