html5地理位置gps传感器禁用watchPosition()

时间:2013-01-11 16:35:46

标签: html5 geolocation gps

我在Andoid 2.3.3上测试地理定位脚本,但是当watchPosition()处于活动状态时,gps传感器会禁用。

我喜欢传感器保持现场状态,因此它会在一秒钟后更新。

这是android 2.3问题还是脚本中的错误?

<!DOCTYPE html>
<html>
  <head>
    <style>
      #tripmeter {
        border: 3px double black;
        padding: 10px;
        margin: 10px 0;
      }

      p {
        color: #222;
        font: 14px Arial;
      }

      span {
        color: #00C;
      }
    </style>
  </head>
  <body>
    <div id="tripmeter">
      <p>
        Starting Location (lat, lon):<br/>
        <span id="startLat">???</span>&deg;, <span id="startLon">???</span>&deg;
      </p>
      <p>
        Current Location (lat, lon):<br/>
        <span id="currentLat">???</span>&deg;, <span id="currentLon">???</span>&deg;
      </p>
      <p>
        Distance from starting location:<br/>
        <span id="distance">0</span> km
      </p>
    </div>
    <script>
      window.onload = function() {
        var startPos;

        if (navigator.geolocation) {
          navigator.geolocation.getCurrentPosition(function(position) {
            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
          }, {enableHighAccuracy: true});

          navigator.geolocation.watchPosition(function(position) {
            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);
          }, {enableHighAccuracy: true});
        }
      };

      // 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-lat1).toRad();
        var dLon = (lon2-lon1).toRad();
        var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
                Math.cos(lat1.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;
      }
      Number.prototype.toRad = function() {
        return this * Math.PI / 180;
      }
    </script>
  </body>
</html>

1 个答案:

答案 0 :(得分:0)

通过在应用程序中的WebView中实现html脚本来解决。凭借清单中的gps权限,传感器保持活动状态。