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 CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

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) {
      console.log("click2");
      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";
    });
  });
}

//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
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console