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

              
                <h2>Using open data to populate a simple map</h2>
<div style="clear: left;">
  <ul>
    <li><a href="https://www.openstreetmap.org/#map=5/54.910/-3.432" target="_blank">Open Street Map</a> tiles</li>
    <li>An open API from <a href="https://openaq.org/" target="_blank">Open AQ</a></li>
    <li><a href="https://plotly.com/javascript/" target="_blank">Plotly</a> library for javascript</li>		 
  </ul>
</div>
<div id="root"></div>
</body>
              
            
!

CSS

              
                
              
            
!

JS

              
                // aqi data from open api openaq.org
var aqi = (function() {
  var json = null;
  $.ajax({
    'async': false,
    'global': false,
     url: "https://api.openaq.org/v1/measurements?country=GB&parameter=pm25",
    'dataType': "json",
    'success': function(data) {
      json = data;
    }
  });
  return json.results;
})();

var mapData = [];
var keys = Object.keys(aqi);
keys.forEach(function(key) {
  var item = aqi[key];
  city = item.city;
  loc = item.location;
  param = item.value;
  lat = item.coordinates.latitude;
  lon = item.coordinates.longitude;
  console.log(city, param, lat, lon);
  result = {
    "city": city,
    "location": loc,
    "param": param,
    "lat": lat,
    "lon": lon
  };
  mapData.push(result);
});

  function getFields(input, field) {
    var output = [];
    for (var i=0; i < input.length ; ++i)
        output.push(input[i][field]);
    return output;
  }

  var allLat = getFields(mapData, "lat");
  var allLon = getFields(mapData, "lon");
  var vals = getFields(mapData, "param");
  var allCity = getFields(mapData, "city");
  var locations = getFields(mapData, "location");

  var map_data = [{
    type:'scattermapbox',
    lat:allLat,
    lon:allLon,
    mode:'markers',
    marker: {
        opacity: 0.75,
        color: 'rgb(178,34,34)', // firebrick
        size: vals,
        sizemode: 'area',
        sizemax: 15,
        sizemin: 0,
        sizeref: 0.1,
    },
    customdata:locations,
    hovertemplate: 'Location: %{customdata}<br><extra></extra>',
  }]

var layout = {
	 title: 'Air quality measurements - PM2.5',
   height: 500,
   hovermode:'closest',
	 font: {color: 'black'},
    dragmode: 'zoom',
    mapbox: {
      center: {lat: 53.3755,lon: -4.1427},
      domain: {x: [0, 1],y: [0, 1]},
      //style: 'stamen-terrain',
      style: 'open-street-map',
      zoom: 4
    },
    margin: {r: 10,t: 50,b: 20,l: 20,pad: 0},
    paper_bgcolor: 'white',
    showlegend: false,
	 annotations: [{x: 0,y: 0, xref: 'paper',yref: 'paper',
		 text: 'Source: <a href="" style="color: rgb(0,0,0)">Open Air Quality API at https://api.openaq.org</a>',
		 showarrow: false
   }]
  };

  Plotly.newPlot('root', map_data, layout);

              
            
!
999px

Console