html5 geolocation



<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8" >
  <title>Geolocation</title>
  <link rel="stylesheet" href="geo-html5.css" >
</head>

<body οnlοad="loadDemo()">

  <header>
    <h1>Geolocation Demo</h1>
    <h4>Live Race Data!</h4>
  </header>

  <div id="container">
  
  <section>
    <article>
      <header>
        <h1>Your Location</h1>
      </header>

        <p class="info" id="status">Geolocation is not supported in your browser.</p>
      
      <div class="geostatus">
        <p id="latitude">Latitude: </p>
        <p id="longitude">Longitude: </p>
        <p id="accuracy">Accuracy: </p>
        <p id="timestamp">Timestamp: </p>
      </div>
      
    </article>
  </section>
     
  <footer>
    <h2>Powered by HTML5</h2>
  </footer>
  
  </div>
   
  <script>
  
    function loadDemo() {
	  
	  if(navigator.geolocation) {
        document.getElementById("status").innerHTML = "HTML5 Geolocation is supported in your browser.";
        navigator.geolocation.getCurrentPosition(updateLocation, handleLocationError,
                                                {timeout:10000});
      }
	}

    function updateLocation(position) {
      var latitude = position.coords.latitude;
      var longitude = position.coords.longitude;
	  var accuracy = position.coords.accuracy;
 	  var timestamp = position.timestamp;

      if (!latitude || !longitude) {
            document.getElementById("status").innerHTML = 
			  "HTML5 Geolocation is supported in your browser, but your location is currently not available.";
        return;
    }
      document.getElementById("latitude").innerHTML = "Latitude: " + latitude;
      document.getElementById("longitude").innerHTML = "Longitude: " +  longitude;
      document.getElementById("accuracy").innerHTML = "Accuracy: " + accuracy + " meters";
      document.getElementById("timestamp").innerHTML = "Timestamp: " + timestamp;
    }

    function handleLocationError(error) {
	  
	  document.getElementById("status").style.background = "papayaWhip";	  	
      switch(error.code)
      {
      case 0:
        updateStatus("There was an error while retrieving your location. Additional details: " + error.message);
        break;
      case 1:
        updateStatus("The user opted not to share his or her location.");
        break;
      case 2:
        updateStatus("The browser was unable to determine your location. Additional details: " + error.message);
        break;
      case 3:
        updateStatus("The browser timed out before retrieving the location.");
        break;
      }
    }
	
	function updateStatus(message) {
      document.getElementById("status").innerHTML = "<strong>Error</strong>: " + message;
    }
	
  </script>

</body>

</html>


<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8" >
  <title>Geolocation</title>
  <link rel="stylesheet" href="geo-html5.css" >
</head>

<body οnlοad="loadDemo()">

  <header>
    <h1>Odometer Demo</h1>
    <h4>Live Race Data!</h4>
  </header>

  <div id="container">
  
  <section>
    <article>
      <header>
        <h1>Your Location</h1>
      </header>
      
        <p class="info" id="status">Geolocation is not supported in your browser.</p>
      
      <div class="geostatus">
        <p id="latitude">Latitude: </p>
        <p id="longitude">Longitude: </p>
        <p id="accuracy">Accuracy: </p>
        <p id="timestamp">Timestamp: </p>
        <p id="currDist">Current distance traveled: </p>
        <p id="totalDist">Total distance traveled: </p>
      </div>
      
    </article>
  </section>

     
  <footer>
    <h2>Powered by HTML5, and your feet!</h2>
  </footer>
  
  </div>
   
  <script>
  
    var totalDistance = 0.0;
    var lastLat;
    var lastLong;

    Number.prototype.toRadians = function() {
      return this * Math.PI / 180;
    }

    function distance(latitude1, longitude1, latitude2, longitude2) {
      // R is the radius of the earth in kilometers
      var R = 6371;

      var deltaLatitude = (latitude2-latitude1).toRadians();
      var deltaLongitude = (longitude2-longitude1).toRadians();
      latitude1 = latitude1.toRadians(), latitude2 = latitude2.toRadians();

      var a = Math.sin(deltaLatitude/2) *
              Math.sin(deltaLatitude/2) +
              Math.cos(latitude1) *
              Math.cos(latitude2) *
              Math.sin(deltaLongitude/2) *
              Math.sin(deltaLongitude/2);

      var c = 2 * Math.atan2(Math.sqrt(a),
                             Math.sqrt(1-a));
      var d = R * c;
      return d;
    }

    function updateErrorStatus(message) {
      document.getElementById("status").style.background = "papayaWhip";	
      document.getElementById("status").innerHTML = "<strong>Error</strong>: " + message;
    }

    function updateStatus(message) {
      document.getElementById("status").style.background = "paleGreen";
      document.getElementById("status").innerHTML = message;
    }

    function loadDemo() {
  
    if(navigator.geolocation) {
        document.getElementById("status").innerHTML = "HTML5 Geolocation is supported in your browser.";
        navigator.geolocation.watchPosition(updateLocation, handleLocationError,
                                                {timeout:10000});
        // updateStatus("Requesting Geolocation data")
      }
	}

    function updateLocation(position) {
      var latitude = position.coords.latitude;
      var longitude = position.coords.longitude;
      var accuracy = position.coords.accuracy;
      var timestamp = position.timestamp;

      document.getElementById("latitude").innerHTML = "Latitude: " + latitude;
      document.getElementById("longitude").innerHTML = "Longitude: " +  longitude;
      document.getElementById("accuracy").innerHTML = "Accuracy: " + accuracy + " meters";
      document.getElementById("timestamp").innerHTML = "Timestamp: " + timestamp;

      // sanity test... don't calculate distance if accuracy
      // value too large
      if (accuracy >= 30000) {
        updateStatus("Need more accurate values to calculate distance.");
        return;
      }

      // calculate distance
      if ((lastLat != null) && (lastLong != null)) {
        var currentDistance = distance(latitude, longitude, lastLat, lastLong);

        document.getElementById("currDist").innerHTML = 
                 "Current distance traveled: " + currentDistance.toFixed(2) + " km";

        totalDistance += currentDistance;
        document.getElementById("totalDist").innerHTML = 
                 "Total distance traveled: " + currentDistance.toFixed(2) + " km";
        updateStatus("Location successfully updated.");

      }

      lastLat = latitude;
      lastLong = longitude;

    }

    function handleLocationError(error) {
      switch(error.code)
      {
      case 0:
        updateErrorStatus("There was an error while retrieving your location. Additional details: " + error.message);
        break;
      case 1:
        updateErrorStatus("The user opted not to share his or her location.");
        break;
      case 2:
        updateErrorStatus("The browser was unable to determine your location. Additional details: " + error.message);
        break;
      case 3:
        updateErrorStatus("The browser timed out before retrieving the location.");
        break;
      }
    }

  </script>

</body>

</html>







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值