Distance Tracker

Sample Project using geolocation

See the full list of sample projects.

This sample file generates a page that displays your real-time distance from Paris

<!DOCTYPE html>
<html>
<head>
  <link href='https://fonts.googleapis.com/css?family=Droid Serif' rel='stylesheet' type='text/css'>
  <style type="text/css">
    html,
    body {
      font-family: "Helvetica";
      height: 100%;
      width: 100%;
      margin: 0;
      color: #ffffff;
    }
    .distance-container {
      background-image: url("paris.jpeg");
      background-size: cover;
      background-position: center;
      background-repeat: no-repeat;
      background-color: #F7F7F7;
      background-blend-mode: multiply;
      height: 100%;
      width: 100%;
      font-size: 1.8em;
      font-weight: 500;
    }
    .current {
      padding: 1rem;
    }
    .away {
      position: absolute;
      bottom: 1rem;
      padding: 1rem;
    }
    .distance {
      font-size: 1.4em;
      font-weight: bold;
    }
    .text-1 {
      font-size: 1em;
      padding: 0.2rem 0;
    }
    .label {
      font-size: 0.9em;
    }
    .lat,
    .lon {
      font-weight: bold;
    }
  </style>
  <meta charset="utf-8"/>
  <meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no"/>
</head>
<body>
  <div class="distance-container">
    <section class="current">
      <div class="text-1">Your current position</div>
      <div><span class="label">longitude: </span><span class="lat"></div>
      <div><span class="label">latitude: </span><span class="lon"></span></div>
    </section>
    <section class="away">
      <div class="text-2">Distance to Paris</div>
      <div class="distance"></div>
    </section>
  </div>

  <script type="text/javascript" src="../lib/motionstack-vlatest.js"></script>
  <script type="text/javascript">
    var lon = document.querySelector(".lon"),
        lat = document.querySelector(".lat"),
        distanceDisplay = document.querySelector(".distance");

    // callback to MotionStack
    var paris = { lat: 48.8584, lon: 2.2945 };

    var geolocation = new MotionStack.Geolocation({
      enableHighAccuracy: true,
      maximumAge: 700,
      timeout: 1000,
      pacemaker: true
    });

    geolocation.start(function(e) {
      if (e.error !== null && e.error.code === e.error.PERMISSION_DENIED) {
        return geolocation.stop();
      };

      var distance = e.distanceTo(paris);
      lon.textContent = e.lon.toFixed(4);
      lat.textContent = e.lat.toFixed(4);
      distanceDisplay.textContent = distance.toFixed(4) + " km";
    });
  </script>
</body>
</html>