Pen Settings

HTML

CSS

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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="container">

  <h1>Geolocation API demo</h1>

  <form class="form-horizontal">
    <div class="form-group">
      <button type="button" class="find-me btn btn-info btn-block">Find your location</button>
    </div>
  </form>

  <p class="not-on-https">Not on HTTPS :( Most browsers require HTTPS to run the Geolocation API.</p>
  <p class="no-geolocation-support">The Geolocation API isn't supported in this browser :(</p>

  <p class="coordinates">Latitude is <b class="latitude">00.000000°</b>, longitude is <b class="longitude">00.000000°</b>.</p>

  <div id="map">
    <div id="map-canvas"></div>
  </div>

</div>

              
            
!

CSS

              
                .container {
  margin: 2rem auto;
  max-width: 50rem;
  text-align: center;
}

form {
  margin: 2.5rem auto;
}

.find-me.btn {
  font-family: inherit;
}

.find-me.btn,
.not-on-https,
.no-geolocation-support,
.coordinates {
  font-size: 1.5em;
}

.not-on-https,
.no-geolocation-support {
  display: none;
}

.coordinates {
  display: none;
  margin-bottom: 2.5rem;
}

.visible {
  display: block;
}

#map {
  margin: 0 auto;
  position: relative;
}

#map-canvas {
  height: 25rem;
  min-height: 25rem;
  width: 100%;
}

              
            
!

JS

              
                $(document).ready(function($) {

  var $findMeBtn = $('.find-me');

  // Check if browser supports the Geolocation API
  if (!navigator.geolocation) {

    $findMeBtn.addClass('disabled');
    $('.no-geolocation-support').addClass('visible');

  // Check if the page is accessed over HTTPS
  } else if (location.protocol !== 'https:') {

    // Check if top-level frame
    if (window.top === window.self) {

      // Reload the page over HTTPS
      location.href = 'https:' + window.location.href.substring(window.location.protocol.length);

    // If not top-level, display a message
      // Note: CodePen does not allow an `<iframe>` to reload the top-level frame (browser window). See about the `sandbox` attribute at https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe#Attributes.
    } else {

      $findMeBtn.addClass('disabled');
      $('.not-on-https').addClass('visible');

    };

  // Let's use the Geolocation API
  } else {

    $findMeBtn.on('click', function(e) {

      e.preventDefault();

      navigator.geolocation.getCurrentPosition(function(position) {

        // Get the location coordinates
        var lat = position.coords.latitude;
        var lng = position.coords.longitude;

        $('.latitude').text(lat.toFixed(6) + '°');
        $('.longitude').text(lng.toFixed(6) + '°');
        $('.coordinates').addClass('visible');

        // Create a map and place a marker at the current location
          // https://developers.google.com/maps/documentation/javascript/reference

        var mapLatLng = new google.maps.LatLng(lat, lng);

        var mapOptions = {
          zoom: 15,
          mapTypeControl: false,
          center: mapLatLng,
        };

        var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

        var mapMarker = new google.maps.Marker({
          position: mapLatLng,
          map: map,
          title: 'Your browser/device places you here',
        });

        // Re-center the map on user location when window/viewport resizes
        $(window).resize(function() {
          google.maps.event.trigger(map, 'resize');
          map.panTo(mapLatLng);
        });

      });

    });

  };

});

              
            
!
999px

Console