phonegap应用仅允许安全来源

时间:2018-12-31 13:38:39

标签: geolocation phonegap-plugins phonegap-build phonegap

我正在开发phonegap android应用 我想获取用户的当前位置,所以我曾经 该插件

 <plugin name="cordova-plugin-geolocation"    source="npm" spec="1.0.1" />  

当我尝试获取用户当前位置时,会在该行中给出该错误

navigator.geolocation.getCurrentPosition(onSuccess, onError);

它调用onError位置并给出该错误

Only secure origins are allowed (see: https://goo.gl /Y0ZkNV).

如何确保电话间隔安全?

1 个答案:

答案 0 :(得分:0)

仅通过 https 服务人员。解决此问题的最简单方法是转到http://localhost:3000而不是计算机的IP地址。

完整代码

<script type="text/javascript">
        app.initialize();

        document.addEventListener("deviceready", onDeviceReady, false);
        function onDeviceReady() {
        console.log(device.cordova);
          var onSuccess = function(position) {

          console.log('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: '         + new Date(position.timestamp)      + '\n');
          };
          function onError(error) {
             console.log('code: '    + error.code    + '\n' +
          'message: ' + error.message + '\n');
          }

          navigator.geolocation.getCurrentPosition(onSuccess, onError);
        }


    </script>

结果:

enter image description here