Javascript 地理位置Cordova的位置未定义
嗨,我刚刚开始学习aparche cordova。 我正在尝试将地理定位api连接到应用程序,并且已经使用Javascript 地理位置Cordova的位置未定义,javascript,android,cordova,Javascript,Android,Cordova,嗨,我刚刚开始学习aparche cordova。 我正在尝试将地理定位api连接到应用程序,并且已经使用cordova plugin add cordova plugin geolocation为cordova安装了地理定位api 我使用的是。 我的JavaScript文件是 // Listening for the device to be ready document.addEventListener("deviceready", onDeviceReady, false); funct
cordova plugin add cordova plugin geolocation为cordova安装了地理定位api
我使用的是。
我的JavaScript文件是
// Listening for the device to be ready
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
console.log("navigator.geolocation should be able to run successfully now...");
navigator.geolocation.getCurrentPosition(onSuccess,onError);
}
// onSuccess Callback
var onSuccess = function(position) {
// current GPS coordinates
if(navigator.geolocation){
console.log('Geolocation is there '+ position);
navigator.geolocation.getCurrentPosition(function(position) {
alert('Latitude: ' + position.coords.latitude + '\n' +
'Longitude: ' + position.coords.longitude + '\n' +
'Altitude: ' + position.coords.altitude + '\n' +
'Accuracy: ' + position.coords.accuracy + '\n' +
'Altitude Accuracy: ' + position.coords.altitudeAccuracy + '\n' +
'Heading: ' + position.coords.heading + '\n' +
'Speed: ' + position.coords.speed + '\n' +
'Timestamp: ' + position.timestamp + '\n');
}, function(error) {
console.log('Error while connecting to geolocation ' + error)
},
{timeout:10000});
}
else {
console.log('Geolocation is not there');
}
}
// onError Callback receives a PositionError object
function onError(error) {
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
}
我在控制台上得到的输出是
地理位置是没有定义的吗
即,位置=未定义。
当onSuccess事件发生时,我没有收到任何警报框。
这段代码在ios上运行得非常好,但在Android/浏览器上不起作用。我遇到了这个问题。这是因为在开发模式下,url是:http/:192.168.0.105
当url具有类似ssl的https://192.168.0.105时,移动设备上的地理定位将起作用
您可以使用以下工具制作假证书:
例如,在类星体框架中,我设置:
devServer: {
https: true,
port: 8080,
open: true // opens browser window automatically
},
此配置解决了我的问题。每当您遇到类似问题时,最可能的原因是应用程序清单中缺少权限。您需要添加
另请参见:我确实添加了android清单上的权限,但警报框仍然弹出。您是否遵循了此指南@Chris G I还尝试使用console.log('Geolocation is there'+position.coords.latitude)在控制台上显示纬度;代码遇到异常“TypeError:无法在onSuccess读取未定义的属性'coords'(file:///android_asset/www/js/index.js:20:55)在HTMLButtonElement.onclick(file:///android_asset/www/index.html:22:58)“
如果位置
已经未定义
,访问位置。coords
也将不起作用。错误信息非常清楚。