cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

Quick-add: + add another resource

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <div style='font-family: sans-serif; text-align:center; font-size:1em;'>
  <input id="btn" type="button" value="What is the nearest Belgian railway station? (we wil not send your location to the server)" onClick="javascript:getClosestStation()" style="width: 100%;height: 50px;border: 2px;font-size:1em;"/>
  <div id="result"></div>
</div>
            
          
!
            
              // First gets geolocation from the browser (end user may have to allow browser to access geolocation).
// Then gets stations list from iRail project and sorts this by distance. It then takes the top element to show to the end user.
function getClosestStation() {
  console.log("getting geolocation");
  navigator.geolocation.getCurrentPosition(function(position) {
    console.log("got geolocation");
    fetch("https://api.irail.be/stations/?format=json").then(function(body) {
      return body.json();
    }).then(function(doc) {
      var stations = doc.station.sort(function(a, b) {
        return haversineDistance({
          latitude: parseFloat(a.locationY),
          longitude: parseFloat(a.locationX)
        }, position.coords) - haversineDistance({
          latitude: parseFloat(b.locationY),
          longitude: parseFloat(b.locationX)
        }, position.coords);
      }, function (e) {
        console.error(e);
        throw "Does not";
      });
      var str = "<p>The closest station to your current location is <em>" + stations[0].name + "</em>.</p><small>Curious what the second closest station is? We already know. Here’s the top 5: ";
      str+= stations[0].name;
      for (var i = 1; i < 5; i++) {
        str+= ", " + stations[i].name;
      }
      str+="</small>";
      document.getElementById("result").innerHTML  = str;
    }).catch(function(err) {
      console.log("error: " + err);
      document.getElementById("result").innerHTML  = "Could not retrieve iRail stations list from your browser";
    });
  }, (error) => {
    console.error(error.message); 
   document.getElementById("result").innerHTML  = "Could not get your current location from your browser I’m afraid. (did you allow me to get your location?)";

  }
  );
}

//Calculates the distance between 2 points on a globe
function haversineDistance(P1, P2) {
  Number.prototype.toRad = function() {
    return this * Math.PI / 180;
  }
  var lat2 = P2.latitude;
  var lon2 = P2.longitude;
  var lat1 = P1.latitude;
  var lon1 = P1.longitude;
  var R = 6371; // km 
  var x1 = lat2 - lat1;
  var dLat = x1.toRad();
  var x2 = lon2 - lon1;
  var dLon = x2.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));
  return R * c;
}

            
          
!
999px
Loading ..................

Console