123

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.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <div class="container">
  <div class="row data">
    <div class="col-sm-6">
      <table id="location" class="table table-hover table-bordered"></table>
    </div>
    <div class="col-sm-6">
      <div id="map"></div>
    </div>
  </div>
  <div class="row note">
    <p>
      Google Chrome only supports navigator.geolocation over HTTPS. Other browsers
      will begin adhering to this best practice in the future as well. 
      This page attempts to display the location reported by navigator.geolocation first.
      If that fails, it then falls back to one of three less-accurate IP-based location APIs.
      Open this page in Chrome over both HTTP and HTTPS to compare location accuracy. You will
      notice a bigger difference in accuracy if connected to a virtual private network, or on 
      a mobile device when not connected to Wi-fi.
    </p>
  </div>  
</div>

            
          
!
            
              body {
  background-color: #222;
  color: #eee;
  font-family: 'verdana', sans-serif;
  font-size: 16px;
  min-width: 280px;
  margin: 0 auto;
  max-width: 1280px;
  padding-top: 3em;
  text-align: center;
}

button {
  color: #222;
}

.table-hover tbody tr:hover td,
.table-hover tbody tr:hover th {
  color: #222;
}

.data .table-bordered td, 
.data .table-bordered th {
  border-color: #444;
  text-align: left;
}

.note {
  margin: 2em 0;
  text-align: left;
}
            
          
!
            
              /* jshint esversion: 6 */

/*
 * Get the location from the user, trying four different methods in case of error.
 * Try the most accurate, navigator.geolocation first. If that fails, try
 * to get IP-based location from ip-api.com. If the API call fails, fall back to 
 * freegeoip.net, and then finally to ipinfo.io. The order of providers chosen is 
 * based on HTTP/HTTPS support and anecdotal ad blocking data.
 *
 * Originally tested with jQuery 3.1.0 on:
 *   - Windows 7 and Windows 10 with Chrome 54 and Firefox 50.
 *   - iOS 10 with Safari and Chrome 55
 *
 */

$(getLocation);

function getLocation() {
  // if navigator.geolocation, attempt to get location, otherwise
  // call the geoError() function which tries IP-based location
  navigator.geolocation
    ? navigator.geolocation.getCurrentPosition(geoSuccess, geoError)
    : geoError();

  // Success. navigator.geolocation is supported and not blocked.
  function geoSuccess(data) {
    displayLoc(
      data.coords.latitude,
      data.coords.longitude,
      "navigator.geolocation"
    );
  }

  // Fail. navigator.geolocation is not supported or was blocked. Use IP-based location insted.
  function geoError() {
    // Try freegeoip.net first
    freegeoip();

    // call freegeoip.net, if it fails, try ipinfo.io
    function freegeoip() {
      $.getJSON("https://freegeoipp.net/json?callback=?")
        .done(data =>
          displayLoc(
            data.latitude,
            data.longitude,
            "https://freegeoip.net/json"
          )
        )
        .fail(ipinfo);
    }

    // call ipinfo.io, if it fails, give an error message.
    function ipinfo() {
      $.getJSON("https://ipinfo.io/json")
        .done(data =>
          displayLoc(
            data.loc.split(",")[0],
            data.loc.split(",")[1],
            "https://ipinfo.io/json"
          )
        )
        .fail(err =>
          // final error message after navigator.geolocation,
          // freegeoip.net, and ipinfo.io have all failed
          $("body").html(
            `<p>Failed to get your location. You may need to disable your Ad Blocker.</p>
           <p>Full Error Text: ${JSON.stringify(err)}</p>`
          )
        );
    }
  }
}

// Display the location results
function displayLoc(lat, lon, provider) {
  const location = lat + "," + lon;

  $("#map").html(
    `<a target="_blank" href="https://maps.google.com/?q=${location}">
       <img src="https://maps.googleapis.com/maps/api/staticmap?center=${location}
                 &zoom=14&size=200x200&scale=2&markers=size:tiny|${location}"
            class="img-responsive center-block">
     </a>`
  );

  $.getJSON(
    "https://maps.googleapis.com/maps/api/geocode/json?latlng=" + location
  ).done(data => {
    $("#location").html(
      `<tbody>
           <tr>
             <th>Types</th>
             <th>Long Name</th>
           </tr>
           <tr>
             <td>Location Provider</td>
             <td>${provider}</td>
           </tr>
         </tbody>`
    );

    data.results[0].address_components.forEach(val => {
      $("#location").append(
        `<tr>
             <td>${val.types[0].replace(/_/g, " ")}</td>
             <td>${val.long_name}</td>
           </tr>`
      );
    });
  });
}

            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console