【问题标题】:Position is undefined for Geolocation CordovaGeolocation Cordova 的位置未定义
【发布时间】:2017-06-29 20:09:54
【问题描述】:

您好,我刚开始学习 aparche cordova。 我正在尝试将地理定位 api 连接到应用程序,并且已经使用 cordova plugin add cordova-plugin-geolocation 为科尔多瓦安装了地理定位 api 并且正在使用apache-cordova documentation 的引用。 我的 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/浏览器上无法运行

【问题讨论】:

  • 每当您遇到此类问题时,最可能的原因是应用清单中缺少权限。您需要添加<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 另请参阅:cordova.apache.org/docs/en/latest/guide/platforms/android/…
  • 我确实在android清单上添加了权限 但是警告框仍然没有弹出
  • @Chris G 我还尝试使用 console.log('Geolocation is there ' + position.coords.latitude); 在控制台上显示纬度并且代码遇到了异常"TypeError: Cannot read property 'coords' of undefined at onSuccess (file:///android_asset/www/js/index.js:20:55) at HTMLButtonElement.onclick (file:///android_asset/www/index.html:22:58)"
  • 如果position 已经是undefined,访问position.coords 也将不起作用。错误信息很清楚。

标签: javascript android cordova


【解决方案1】:

我遇到了这个问题。这是因为在开发模式下,url 是:http//:192.168.0.105

当网址具有像 https://192.168.0.105

这样的 ssl 时,移动工作的地理位置

您可以使用以下方法制作假证书:

https://blog.filippo.io/mkcert-valid-https-certificates-for-localhost/

例如,在quasar框架我设置:

 devServer: {
      https: true,
      port: 8080,
      open: true // opens browser window automatically
    }, 

而这个配置解决了我的问题。

【讨论】:

    猜你喜欢
    • 2015-05-07
    • 1970-01-01
    • 2017-09-07
    • 2015-12-29
    • 2017-11-30
    • 2018-10-29
    • 1970-01-01
    • 2012-08-14
    • 1970-01-01
    相关资源
    最近更新 更多