我可以使用app.initialize从jquery构造中执行getCurrentPosition吗

时间:2019-05-23 18:54:49

标签: jquery ios cordova safari geolocation

使用jquery构造进行初始化,getCurrentPosition / watchPosition在iOS Safari或Xcode模拟器上不再起作用。

此代码在5月之前有效。我删除了大多数代码以测试基本的navigator.geolocation.getCurrentPosition功能。如果将getCurrentPosition代码从Historigin对象/初始化函数中拉出并将其放在最后一行之外,则getCurrentPosition将起作用。照原样,我当前得到的结果是:ERROR(3):位置检索超时。

Main.html

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<meta name="format-detection" content="telephone=no" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0"/>
<script src="https://maps.googleapis.com/maps/api/js?key=YOURAPIKEY">
</script>
<script type="text/javascript" src="js/Libraries/myphp.min.js"></script>
<script type="text/javascript" src="js/Libraries/jquery.js"></script>
<script type="text/javascript" src="js/Libraries/jquery.mobile-events.min.js"></script>
<script type="text/javascript" src="js/Libraries/jquery.touchSwipe.min.js"></script>
<script type="text/javascript" src="js/config.js"></script>
<script type="text/javascript" src="js/main.js"></script>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="cordova_plugins.js"></script>
<script type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" src="js/index.js"></script>
<script type="text/javascript" src="statusbarnotification.js"></script>
<script src="https://cdn.jsdelivr.net/npm/promise-polyfill@8/dist/polyfill.min.js"></script>

<link rel="stylesheet" type="text/css" href="css/_reset.css" />
<link rel="stylesheet" type="text/css" href="css/index.css" />

</head>
<body>

<script>
$(function() {
  document.addEventListener("deviceready", onDeviceReady, false);
  function onDeviceReady() {
  Historigin.initialize();
  }
  app.initialize();
  });
</script>
</body>
</html>

main.js

var Historigin = {
initialize: function() {

    var options = {
    enableHighAccuracy: true,
    timeout: 5000,
    maximumAge: 0
    };
    function success(pos) {
        var crd = pos.coords;
        console.log('Your current position is:');
        console.log(`Latitude : ${crd.latitude}`);
        console.log(`Longitude: ${crd.longitude}`);
        console.log(`More or less ${crd.accuracy} meters.`);
    }

    function error(err) {
        console.warn(`ERROR(${err.code}): ${err.message}`);
    }

    navigator.geolocation.getCurrentPosition(success, error, options);
}
};

我希望将当前位置的经纬度输出到console.log。

0 个答案:

没有答案