【问题标题】:Not able to get the geolocation in Android emulator无法在 Android 模拟器中获取地理位置
【发布时间】:2014-11-05 09:34:46
【问题描述】:

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

<!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

【问题讨论】:

  • 在安卓模拟器中你无法使用真实设备获取地理位置。
  • telnet 有办法在 android 模拟器中修复地理,但这对我也不起作用。genymotion 怎么样?我们可以得到其中的地理位置吗?我们也可以将 bin\android\debug\App2-debug-unaligned.apk 直接安装到设备上吗?(我无法测试它,因为我现在没有安卓设备)

标签: android visual-studio cordova multi-device-hybrid-apps


【解决方案1】:

我发现了问题所在。 如果我使用此代码,它工作正常

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

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

【讨论】:

    【解决方案2】:

    我正在将 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);
    });
    

    Code Pen Version

    【讨论】:

      猜你喜欢
      • 2012-01-26
      • 1970-01-01
      • 1970-01-01
      • 2017-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多