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. 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

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

Details

Privacy

Go PRO Window blinds lowered to protect code. Code Editor with window blinds (raised) and a light blub turned on.

Keep it secret; keep it safe.

Private Pens are hidden everywhere on CodePen, except to you. You can still share them and other people can see them, they just can't find them through searching or browsing.

Upgrade to PRO

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.

Template

Make Template?

Templates are Pens that can be used to start other Pens. You can view all of your templates, or learn more in the documentation.

Screenshot

Screenshots of Pens are shown in mobile browsers, RSS feeds, to users who chose images instead of iframes, and in social media sharing.

This Pen is using the default Screenshot, generated by CodePen. Upgrade to PRO to upload your own thumbnail that will be displayed on previews of this pen throughout the site and when sharing to social media.

Upgrade to PRO

HTML

              
                <html lang="en">
  <head>
    <title>test format kml</title>
  </head>
  <body>
    <div id="map" class="map"></div>
    <div>
      <label>draw</label>
      <input type="radio" id="interaction_type_draw" name="interaction_type" value="draw" checked>
    </div>
    <div>
      <label>Geometry type</label>
      <select id="geom_type">
        <option value="LineString" selected>LineString</option>
      </select>
    </div>
  </body>
</html>
              
            
!

CSS

              
                .map {
  height: 400px;
  width: 600px;
}
              
            
!

JS

              
                // create a vector layer used for editing
var vector_layer = new ol.layer.Vector({
  name: 'my_vectorlayer',
  source: new ol.source.Vector(),
  style: new ol.style.Style({
    fill: new ol.style.Fill({
      color: 'rgba(255, 255, 255, 0.2)'
    }),
    stroke: new ol.style.Stroke({
      color: '#ffcc33',
      width: 2
    }),
    image: new ol.style.Circle({
      radius: 7,
      fill: new ol.style.Fill({
        color: '#ffcc33'
      })
    })
  })
});

// Create a map
var map = new ol.Map({
  target: 'map',
  layers: [
    new ol.layer.Tile({
      source: new ol.source.OSM()
    }),
    vector_layer
  ],
  view: new ol.View({
    zoom: 2,
    center: [0, 0]
  })
});

// make interactions global so they can later be removed
var select_interaction,
    draw_interaction;

// get the interaction type
var $interaction_type = $('[name="interaction_type"]');
// rebuild interaction when changed
$interaction_type.on('click', function(e) {
  // add new interaction
  if (this.value === 'draw') {
    addDrawInteraction();
  }
});

// get geometry type
var $geom_type = $('#geom_type');
// rebuild interaction when the geometry type is changed
$geom_type.on('change', function(e) {
  map.removeInteraction(draw_interaction);
  addDrawInteraction();
});

// get data type to save in
$data_type = $('#data_type');
// clear map and rebuild interaction when changed
$data_type.onchange = function() {
  clearMap();
  map.removeInteraction(draw_interaction);
  addDrawInteraction();
};

// creates a draw interaction
function addDrawInteraction() {
  // remove other interactions
  map.removeInteraction(select_interaction);
  
  // create the interaction
  draw_interaction = new ol.interaction.Draw({
    source: vector_layer.getSource(),
    type: /** @type {ol.geom.GeometryType} */ ($geom_type.val())
  });
	// add it to the map
  map.addInteraction(draw_interaction);

  // when a new feature has been drawn...
  draw_interaction.on('drawstart', function(evt) {
    var feature = evt.feature;
    var geom = feature.getGeometry();
    document.addEventListener('keyup', function() {
      if (event.keyCode === 27) {
        if (geom.getType() === "LineString") {
          var coords = geom.getCoordinates();
          var len = coords.length;
          console.log("undo");
          if (len > 1) {
           geom.setCoordinates(geom.getCoordinates().slice(0, len - 1));
          }
        }
      }
    });
  });
  
  // when a new feature has been drawn...
  draw_interaction.on('drawend', function(event) {
    // create a unique id
    // it is later needed to delete features
    var id = uid();
    // give the feature his id
    event.feature.setId(id);
  });
}

// add the draw interaction when the page is first shown
addDrawInteraction();

// clears the map and the output of the data
function clearMap() {
  vector_layer.getSource().clear();
  if (select_interaction) {
  	select_interaction.getFeatures().clear();
  }
  $('#data').val('');
}

// creates unique id's
function uid(){
  var id = 0;
  return function() {
    if (arguments[0] === 0) {
      id = 0;
    }
    return id++;
  }
}
              
            
!
999px
Be the developer your linter thinks you are.

Console