Cordova Geolocation返回NAN的纬度和经度

时间:2016-08-03 19:19:30

标签: javascript html windows cordova geolocation

所以我是javascript和Cordova的新手,我正在尝试在Visual Studio 2015中创建一个简单的GPS应用程序,当我点击“当前位置”时,它就会获得我当前的位置。

当我在Firefox中测试时,它可以工作,但是当我尝试在本地计算机或远程计算机上运行它时,它不起作用。 (我正在使用Windows 10并在Panasonic FZ-M1 Toughpad上进行测试。当我在本地/远程机器上运行时,经度和经度在大约30秒后返回NAN。

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">
    <meta name="format-detection" content="telephone=no">
    <meta name="msapplication-tap-highlight" content="no">
    <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
    <link rel="stylesheet" type="text/css" href="css/index.css">
    <script src="http://maps.google.com/maps/api/js?sensor=true"></script>
    <title>gpsCordova</title>
</head>
<body>
    <div class="app">
        <h1>Apache Cordova</h1>
        <div id="deviceready" class="blink">
            <p class="event listening">Connecting to Device</p>
            <p class="event received">Device is Ready</p>
        </div>
    </div>
    <h2>Touch Me.</h2>
    <button id="locationButton">CURRENT LOCATION</button><br>
    <p id="Geolocation"></p>

    <script type="text/javascript" src="cordova.js"></script>
    <script type="text/javascript" src="scripts/platformOverrides.js"></script>
    <script type="text/javascript" src="scripts/main.js"></script>

</body>

document.getElementById("locationButton").addEventListener("click", getLocation);

var geoLoc = document.getElementById("Geolocation");
var geolocationOptions = {
enableHighAccuracy: true,
maximumAge: 3600000
}

function getLocation() {
navigator.geolocation.getCurrentPosition(geolocationSuccess, geolocationError, geolocationOptions);
}

function geolocationSuccess(position) {

geoLoc.innerHTML ="Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude +
"<br>Accuracy: " + position.coords.accuracy +
"<br>Altitude: " + position.coords.altitude +
"<br>Altitude Accuracy: " + position.coords.altitudeAccuracy +
"<br>Heading: " + position.coords.heading +
"<br>Speed: " + position.coords.speed +
"<br>Timestamp: " + position.coords.timestamp + "<br>";

}

function geolocationError(error) {
geoLoc.innerHTML = "Failure" +
   "<br>error: " + error.name +
   "<br>message: " + error.message;
    }

我不确定还有什么要补充的,但是有没有人对此问题有任何经验?或者我的代码中缺少什么?

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

此插件提供的功能在deviceready事件发生后才可用。

  

https://github.com/apache/cordova-plugin-geolocation