<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Peter Cook</title>
    <description>The latest articles on DEV Community by Peter Cook (@createwithdata).</description>
    <link>https://dev.to/createwithdata</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F197966%2F839511ec-f5af-4f14-bd71-b55e18b464e0.jpg</url>
      <title>DEV Community: Peter Cook</title>
      <link>https://dev.to/createwithdata</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/createwithdata"/>
    <language>en</language>
    <item>
      <title>How to make a data-driven map: Part One</title>
      <dc:creator>Peter Cook</dc:creator>
      <pubDate>Fri, 19 Jul 2019 15:47:26 +0000</pubDate>
      <link>https://dev.to/createwithdata/make-a-data-driven-map-part-one-31h8</link>
      <guid>https://dev.to/createwithdata/make-a-data-driven-map-part-one-31h8</guid>
      <description>&lt;p&gt;&lt;em&gt;Cross posted from &lt;a href="https://www.createwithdata.com/make-a-data-driven-map-part-one/" rel="noopener noreferrer"&gt;Create With Data&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This four part tutorial shows how to make a data-driven map using JavaScript.&lt;/p&gt;

&lt;p&gt;We'll visualise electric vehicle chargepoint locations on a map:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://codepen.io/createwithdata/live/ewoVGE" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.createwithdata.com%2Fimages%2Fmake-a-data-driven-map%2Ffinished.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The data comes from the UK's &lt;a href="https://www.national-charge-point-registry.uk/" rel="noopener noreferrer"&gt;National Chargepoint Registry&lt;/a&gt; and we'll use &lt;a href="https://leafletjs.com/" rel="noopener noreferrer"&gt;Leaflet&lt;/a&gt; to generate the map. Leaflet is an open source JavaScript library for creating interactive maps. It's a bit like Google Maps, but open source.&lt;/p&gt;

&lt;p&gt;We'll use CodePen to develop our code as it requires minimal set-up so you can be up and running quickly.&lt;/p&gt;

&lt;h2&gt;
  
  
  What to expect
&lt;/h2&gt;

&lt;p&gt;In Part One you'll learn how to create the background map using Leaflet.&lt;/p&gt;

&lt;p&gt;Part Two will cover loading CSV data and plotting the locations on the map.&lt;/p&gt;

&lt;p&gt;Part Three will cover styling the points in a data-driven fashion. For example, the points will be coloured according to the operator of each charge point.&lt;/p&gt;

&lt;p&gt;The final part will add a title, legend and an information pop-up for when a point is hovered.&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting started
&lt;/h2&gt;

&lt;p&gt;Go to &lt;a href="https://codepen.io/" rel="noopener noreferrer"&gt;CodePen&lt;/a&gt; and create a new pen. (If you're not familiar with CodePen, check out my &lt;a href="https://www.createwithdata.com/visualising-data-with-js-getting-started/" rel="noopener noreferrer"&gt;Visualising Data with JavaScript tutorial&lt;/a&gt;.)&lt;/p&gt;

&lt;p&gt;Clicking on the cog in the JS panel and add &lt;code&gt;https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.4.0/leaflet.js&lt;/code&gt; to the 'Add External Scripts/Pens' list:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.createwithdata.com%2Fimages%2Fcodepen-include-leaflet.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.createwithdata.com%2Fimages%2Fcodepen-include-leaflet.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In a similar manner, click on the cog in the CSS panel and add &lt;code&gt;https://unpkg.com/leaflet@1.4.0/dist/leaflet.css&lt;/code&gt; to the 'Add External Stylesheets/Pens' list.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.createwithdata.com%2Fimages%2Fcodepen-include-leaflet-css.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.createwithdata.com%2Fimages%2Fcodepen-include-leaflet-css.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Finally click 'Change View' and select the arrangement with the output window on the right:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.createwithdata.com%2Fimages%2Fcodepen-change-view.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.createwithdata.com%2Fimages%2Fcodepen-change-view.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This arrangement will suit your chart better.&lt;/p&gt;

&lt;h2&gt;
  
  
  Create a map
&lt;/h2&gt;

&lt;p&gt;There are three steps to creating a map using Leaflet:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;add an HTML element to contain the map&lt;/li&gt;
&lt;li&gt;add a CSS rule to set the size of the map&lt;/li&gt;
&lt;li&gt;add JavaScript to create the map&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Add map container
&lt;/h3&gt;

&lt;p&gt;In the HTML panel add a &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; element to act as a container for the map. Give it an id of &lt;code&gt;map&lt;/code&gt; so that we can reference it from Leaflet:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"map"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Set the size of the container
&lt;/h3&gt;

&lt;p&gt;It's important to set the height of the container otherwise your map will be invisible!&lt;/p&gt;

&lt;p&gt;We'd like our map to fill the window so add the following style rules to the CSS panel:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;html&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nt"&gt;body&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;100%&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;margin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;#map&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;100%&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;You can set the width and height of &lt;code&gt;#map&lt;/code&gt; to whatever you like. For example, try setting the width and height to &lt;code&gt;400px&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Create the map
&lt;/h3&gt;

&lt;p&gt;Typically a Leaflet map consists of a map object and a number of layers. Leaflet supports a number of layer types including: tiles, markers, polygons, GeoJSON and more.&lt;/p&gt;

&lt;p&gt;Let's start by creating a map object. In the JavaScript panel add:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;map&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;L&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;map&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now create a tile layer and add it to the map:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;tileLayer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;L&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;tileLayer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;attribution&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;&amp;amp;copy; &amp;lt;a href="https://www.openstreetmap.org/copyright"&amp;gt;OpenStreetMap&amp;lt;/a&amp;gt; contributors&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;tileLayer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addTo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Our code is using Open Street Map's tile server but you can choose from a number of different servers. A great resource for exploring available servers is the [Leaflet Provider Demo](&lt;a href="https://leaflet-extras.github.io/leaflet-providers/preview" rel="noopener noreferrer"&gt;https://leaflet-extras.github.io/leaflet-providers/preview&lt;/a&gt;. Note that different tile providers have different terms of use. If your map is likely to receive a lot of traffic then it's important to check that you're staying within any usage limits. It's also important to add attribution for whichever provider you're using.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A tile layer connects with a map tile server and requests tiles for a given location and zoom level. Each tile is a small image e.g.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.createwithdata.com%2Fimages%2Fmake-a-data-driven-map%2Fmap-tile.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.createwithdata.com%2Fimages%2Fmake-a-data-driven-map%2Fmap-tile.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When Leaflet receives each tile it places them in a grid layout to make up the map.&lt;/p&gt;

&lt;p&gt;Now set the view location and zoom level of the map using &lt;code&gt;map.setView()&lt;/code&gt;. The first argument is an array &lt;code&gt;[latitude, longitude]&lt;/code&gt; and the second argument a zoom level. The higher the zoom level the more zoomed in the map is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setView&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;55&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There should now be a map in the output pane:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.createwithdata.com%2Fimages%2Fmake-a-data-driven-map%2Fbasemap-1.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.createwithdata.com%2Fimages%2Fmake-a-data-driven-map%2Fbasemap-1.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note that you can zoom and pan around the map (in a similar manner to Google Maps).&lt;/p&gt;

&lt;h2&gt;
  
  
  Styling the map
&lt;/h2&gt;

&lt;p&gt;Although you can change the style of the basemap by choosing a different tile provider (see previous section) you can also style the tiles using CSS filters.&lt;/p&gt;

&lt;p&gt;To make the map grayscale add the following to the CSS panel:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.leaflet-container&lt;/span&gt; &lt;span class="nc"&gt;.leaflet-tile-pane&lt;/span&gt; &lt;span class="nt"&gt;img&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;grayscale&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This'll make the coloured circles we'll add in Part Two stand out more.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.createwithdata.com%2Fimages%2Fmake-a-data-driven-map%2Fbasemap.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.createwithdata.com%2Fimages%2Fmake-a-data-driven-map%2Fbasemap.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here's the finished code on Codepen:&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/createwithdata/embed/ZdZvXY?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapping up
&lt;/h2&gt;

&lt;p&gt;We've created a Leaflet map and made it grayscale by adding a CSS rule.&lt;/p&gt;

&lt;p&gt;In the next part we'll load in the data (using &lt;a href="https://d3js.org/" rel="noopener noreferrer"&gt;D3&lt;/a&gt; and draw each data point on the map.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.createwithdata.com%2Fimages%2Fmake-a-data-driven-map%2Fpoints.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.createwithdata.com%2Fimages%2Fmake-a-data-driven-map%2Fpoints.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the meantime have a play around with the map. Some things you can try are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;explore the other tile providers from &lt;a href="https://leaflet-extras.github.io/leaflet-providers/preview" rel="noopener noreferrer"&gt;Leaflet Provider Demo&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;resize the map (using CSS)&lt;/li&gt;
&lt;li&gt;style the map (using CSS filters). Other filters include &lt;code&gt;opacity&lt;/code&gt; and &lt;code&gt;sepia&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Happy mapping and I hope to see you in the next part!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>map</category>
      <category>dataviz</category>
      <category>leafletjs</category>
    </item>
  </channel>
</rss>
