【问题标题】:Android Nexus 7 Enable GPS SensorAndroid Nexus 7 启用 GPS 传感器
【发布时间】:2014-10-22 14:52:04
【问题描述】:

我刚刚开始使用 HTML5 和 Phonegap 进行一些应用程序开发。我正在尝试使用 GPS(禁用 Wifi)来显示 lon/lat 坐标。代码工作......有点。

如果我打开应用程序,我无法获得位置信息。如果我打开任何其他使用地理定位的应用程序——谷歌地图或 GPS 测试仪——然后切换回我的应用程序,它就会工作并更新坐标。就像我的应用没有打开 GPS 传感器,但一旦打开,我的应用就可以访问它。

我错过了什么吗?

代码;

<script>
  window.onload = function() {
    var startPos;

    if (navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(function(position) {
        enableHighAccuracy: true;
        startPos = position;
        document.getElementById("startLat").innerHTML = startPos.coords.latitude;
        document.getElementById("startLon").innerHTML = startPos.coords.longitude;
      }, function(error) {
        alert("Error occurred. Error code: " + error.code);
        // error.code can be:
        //   0: unknown error
        //   1: permission denied
        //   2: position unavailable (error response from locaton provider)
        //   3: timed out
      });

      navigator.geolocation.watchPosition(function(position) {
        enableHighAccuracy: true;
        document.getElementById("currentLat").innerHTML = position.coords.latitude;
        document.getElementById("currentLon").innerHTML = position.coords.longitude;
        document.getElementById("distance").innerHTML =
          calculateDistance(startPos.coords.latitude, startPos.coords.longitude,
                            position.coords.latitude, position.coords.longitude);
      });
    }
  };

  // Reused code - copyright Moveable Type Scripts - retrieved May 4, 2010.
  // http://www.movable-type.co.uk/scripts/latlong.html
  // Under Creative Commons License http://creativecommons.org/licenses/by/3.0/
  function calculateDistance(lat1, lon1, lat2, lon2) {
    var R = 6371; // km
    var dLat = (lat2-51.202399).toRad();
    var dLon = (lon2-parseFloat(-1.479645)).toRad();
    var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
            Math.cos(51.202399.toRad()) * Math.cos(lat2.toRad()) *
            Math.sin(dLon/2) * Math.sin(dLon/2);
    var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
    var d = R * c;
    return d.toFixed(2);
  }
  Number.prototype.toRad = function() {
    return this * Math.PI / 180;
  }
</script>

最好的问候 安迪

【问题讨论】:

    标签: html gps


    【解决方案1】:

    使用 org.apache.cordova.geolocation 插件。

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

    【讨论】:

    • 谢谢!原谅我的无知,但这对我来说很新鲜。我只是使用 Dreamweaver 来编辑我的代码。然后我将其压缩,上传到 build.phonegap.com 并在我的设备上安装该应用程序。我不明白这个插件是如何适合的,或者在哪里?你能指出我正确的方向吗?
    • 啊,你用的是PhoneGap Build。好的,阅读此页面 docs.build.phonegap.com/en_US/…build.phonegap.com/plugins/1176
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-06
    • 2010-12-31
    • 2014-05-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多