Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs 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 its URL and the proper URL extension.

+ 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

Auto Save

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 id="map"></div>
<div id="clear">
  <button onClick="clearRoute()">CLEAR</button>
</div>
<div id="myChart"></div>

              
            
!

CSS

              
                body { margin: 0; padding: 0; }
#map { 
 position: absolute;
  top: 0;
  bottom: 0;
  right: 0;
  left: 0;
}
#myChart {
  position: absolute;
  bottom: 0;
  right: 0;
  width: 400px;
  height: 100px;
}
#clear {
  position: absolute;
  top: 0;
  margin: 6px 6px;
  right: 0;
}
              
            
!

JS

              
                let chartData = []
drawChart()

const map = new maplibregl.Map({
  container: "map",
  zoom: 15,
  center: [-80.9, 35.47],
  pitch: 45,
  maxPitch: 70,
  minZoom: 9,
  style: {
    version: 8,
    name: "OSM Mecklenburg GeoPortal",
    maxPitch: 70,
    zoom: 13.5,
    center: [-80.846, 35.223],
    sources: {
      osm: {
        type: "raster",
        tiles: ["https://a.tile.openstreetmap.org/{z}/{x}/{y}.png"],
        tileSize: 256,
        attribution: "&copy; OpenStreetMap Contributors",
        maxzoom: 19
      },
      hillshade_source: {
      type: "raster-dem",
        encoding: "terrarium",
        tiles: [
          "https://s3.amazonaws.com/elevation-tiles-prod/terrarium/{z}/{x}/{y}.png"
        ],
        tileSize: 256,
        minzoom: 0,
        maxzoom: 14
    },
      terrain_source: {
        type: "raster-dem",
        encoding: "terrarium",
        tiles: [
          "https://s3.amazonaws.com/elevation-tiles-prod/terrarium/{z}/{x}/{y}.png"
        ],
        tileSize: 256,
        minzoom: 0,
        maxzoom: 14
      }
    },
    layers: [
      {
        id: "osm",
        type: "raster",
        source: "osm"
      },
      {
        id: "hills",
        type: "hillshade",
        source: "hillshade_source"
      }
    ],
    terrain: {
      source: 'terrain_source',
      exaggeration: 5
    }
  }
});

function drawChart() {
  var myConfig = {
    type: "line",
    "scale-x": {
      "line-color": "none",
      item: {
        visible: false
      },
      tick: {
        "line-color": "none"
      }
    },
    "scale-y": {
      "line-color": "none",
      item: {
        visible: false
      },
      tick: {
        "line-color": "none"
      }
    },
    plotarea: {
      margin: "20 20"
    },
    series: [{ values: chartData }],
    plot: {
      aspect: "spline"
    }
  };

  zingchart.render({
    id: "myChart",
    data: myConfig,
    height: "100%",
    width: "100%"
  });
}

function clearRoute() {
  geojson.features = []
  map.getSource("geojson").setData(geojson)
  chartData = []
  drawChart()
}

// GeoJSON object to hold our measurement features
var geojson = {
  type: "FeatureCollection",
  features: []
};

// Used to draw a line between points
var linestring = {
  type: "Feature",
  geometry: {
    type: "LineString",
    coordinates: []
  }
};

map.on("load", function () {
  map.addSource("geojson", {
    type: "geojson",
    data: geojson
  });

  // Add styles to the map
  map.addLayer({
    id: "measure-points",
    type: "circle",
    source: "geojson",
    paint: {
      "circle-radius": 5,
      "circle-color": "#000"
    },
    filter: ["in", "$type", "Point"]
  });
  map.addLayer({
    id: "measure-lines",
    type: "line",
    source: "geojson",
    layout: {
      "line-cap": "round",
      "line-join": "round"
    },
    paint: {
      "line-color": "#000",
      "line-width": 2.5
    },
    filter: ["in", "$type", "LineString"]
  });

  map.on("click", function (e) {
    var features = map.queryRenderedFeatures(e.point, {
      layers: ["measure-points"]
    });

    // Remove the linestring from the group
    // So we can redraw it based on the points collection
     if (geojson.features.length > 1) geojson.features.pop();

    // If a feature was clicked, remove it from the map
    if (features.length) {
      var id = features[0].properties.id;
      geojson.features = geojson.features.filter(function (point) {
        return point.properties.id !== id;
      });
    } else {
      var point = {
        type: "Feature",
        geometry: {
          type: "Point",
          coordinates: [e.lngLat.lng, e.lngLat.lat]
        },
        properties: {
          id: String(new Date().getTime())
        }
      };
      geojson.features.push(point);
    }

    if (geojson.features.length > 1) {
      linestring.geometry.coordinates = geojson.features.map(function (point) {
        return point.geometry.coordinates;
      });

      geojson.features.push(linestring);
      
      // get length of line
      let lineLength = turf.length(linestring, {units: 'meters'})
      
      // however many subdivisions we want
      let divisionLength = lineLength / 20
      
      let newLine = turf.lineChunk(linestring, divisionLength, {units: 'meters'})
      chartData = newLine.features.map(el => map.queryTerrainElevation(el.geometry.coordinates[0]))
      drawChart();
    }

    map.getSource("geojson").setData(geojson);
  });
});

              
            
!
999px

Console