3

我正在做一个小示例应用程序,当我单击按钮时,它会在弹出窗口中显示纬度和经度这是我的代码

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>BlankCordovaApp1</title>

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

    <script src="cordova.js"></script>
    <script src="scripts/platformOverrides.js"></script>
    <script src="scripts/index.js"></script>

    <script type="text/javascript" charset="utf-8">
        var alertmsg = function (position) {
            var msg = 'Latitude: ' + position.coords.latitude + '<br />' +
                'Longitude: ' + position.coords.longitude + '<br />'
            navigator.notification.alert(msg);
        }
        function geoLocation() {
            navigator.geolocation.getCurrentPosition(alertmsg)
        }
    </script>
</head>
<body>
    <input type="button" id="btnClick" onclick="geoLocation()" value="click" />
</body>
</html>

它在 Ripple 模拟器中运行但在 Android 模拟器和 Genymotion 中不起作用

4

2 回答 2

1

我解决了这个问题。如果我使用此代码,它工作正常

navigator.geolocation.getCurrentPosition(alertmsg, onError, { timeout: 30000, enableHighAccuracy: true });

它适用于所有模拟器(Ripple、Android、Genymotion)

于 2014-11-05T16:45:09.890 回答
1

我将 Visual Studio 13 与基于 Backbone 的智能手机应用程序一起使用,这很痛苦。添加超时选项并enableHighAccuracy始终抛出onError处理程序,没有这些都不会返回。

所以这是一个很好的答案:

//Android Emulator safe version
function getGpsCordinates(callback) {
    if ("geolocation" in navigator) {                  
        navigator.geolocation.getCurrentPosition(
            //Success
            function (position) {
                console.log("GPS: Success");
                callback(position);
            },
            //Error
            function (error) {
                console.log("GPS: Error");
                var position = {
                  coords: {
                      longitude: 0,
                      latitude: 0,
                      speed: 0
                  }
              };
              callback(position);
            },
            { timeout: 7000, enableHighAccuracy: true });
    } else {
        var position = {
            coords: {
                longitude: 0,
                latitude: 0,
                speed: 0
            }
        };
        console.log("GPS: Not Supported");
        callback(position);
    }
    console.log("GPS: Continued");
}

getGpsCordinates(function(mycallback) {
  alert(mycallback.coords.latitude);
});

代码笔版

于 2015-05-20T08:48:37.223 回答