|
| 1 | +# Leaflet Maps |
| 2 | + |
| 3 | +Let's see what we can do with an interactive geographical map within TypeCell. |
| 4 | +For this demo we will use the <a href="https://leafletjs.com/" target="_blank">Leaflet</a> library to display our map. |
| 5 | + |
| 6 | +According to the Leaflet documentation we first need to load the css stylesheet before we can use the map. We can import the stylesheet using a css codeblock. You can change the codeblock type in the bottom right corner. |
| 7 | + |
| 8 | +```css |
| 9 | +@import url( "https://unpkg.com/[email protected]/dist/leaflet.css"); |
| 10 | +``` |
| 11 | + |
| 12 | +Next we will setup a basic map using the Leaflet library with openstreetmap. |
| 13 | +Simply import the leaflet library and add the required configuration according to the Leaflet documentaiton. |
| 14 | +In this case I've added the code for generating the map in an exported function in order to reuse it later. |
| 15 | + |
| 16 | +It's necessary to provide Leaflet with a tile provider. For this demo I've used the `OpenStreetMap.Mapnik` provider. |
| 17 | +There are plenty of tile options to choose from (Find more tile providers <a href="https://leaflet-extras.github.io/leaflet-providers/preview/" target="_blank">here</a>). |
| 18 | + |
| 19 | +```typescript |
| 20 | +import * as L from "leaflet"; |
| 21 | + |
| 22 | +export function generateMap() { |
| 23 | + const container = document.createElement("div"); |
| 24 | + container.style.cssText = "width: 100%; height: 400px"; |
| 25 | + const map = L.map(container).setView([48.505, 17.09], 4); |
| 26 | + |
| 27 | + L.tileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", { |
| 28 | + maxZoom: 19, |
| 29 | + attribution: |
| 30 | + '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors', |
| 31 | + }).addTo(map); |
| 32 | + |
| 33 | + return { element: container, map }; |
| 34 | +} |
| 35 | + |
| 36 | +export default generateMap().element; |
| 37 | +``` |
| 38 | + |
| 39 | +Voila! We now have a working map that we can interact with in TypeCell. |
| 40 | + |
| 41 | +## Add airport locations âœˆï¸ |
| 42 | + |
| 43 | +What fun is a map without markers? Let's add the biggest airports in Europe to a map. |
| 44 | +I've found a csv file that lists the locations of all airports worldwide which we can use to find all airport locations. |
| 45 | + |
| 46 | +Using the native `fetch` function we can download the entire csv file as a string. Next, we parse the csv file using `papaparse`. Simply import the module and use the `parse` function to parse the csv into JavaScript objects. Finally, filter the airports by continent and airport type and export it. |
| 47 | + |
| 48 | +```typescript |
| 49 | +import * as Papa from "papaparse"; |
| 50 | + |
| 51 | +const airportCsv = await fetch( |
| 52 | + "https://davidmegginson.github.io/ourairports-data/airports.csv", |
| 53 | +).then((response) => response.text()); |
| 54 | + |
| 55 | +// Pass the row type to the parser |
| 56 | +const { data } = Papa.parse<{ |
| 57 | + latitude_deg: string; |
| 58 | + longitude_deg: string; |
| 59 | + continent: string; |
| 60 | + type: string; |
| 61 | +}>(airportCsv, { |
| 62 | + header: true, |
| 63 | + transformHeader: (header) => header.toLowerCase().split(" ").join(""), |
| 64 | +}); |
| 65 | + |
| 66 | +// Filter and export airports |
| 67 | +export const airports = data.filter( |
| 68 | + (airport) => airport.continent === "EU" && airport.type === "large_airport", |
| 69 | +); |
| 70 | +``` |
| 71 | + |
| 72 | +As you can see, all the airports are exported above. You are able to inspect all exported airports by clicking on the little arrow in front of `airports` above. From now on we can use `$.airports` in other code blocks. |
| 73 | + |
| 74 | +Let's prepare the code for a new map with the airport markers. We can use the `generateMap` function that we created before to create a new map and loop over all the airports to add a marker for each one. |
| 75 | + |
| 76 | +```typescript |
| 77 | +import * as L from "leaflet"; |
| 78 | + |
| 79 | +const { element, map } = $.generateMap(); |
| 80 | + |
| 81 | +for (let airport of $.airports) { |
| 82 | + L.marker([+airport.latitude_deg, +airport.longitude_deg]).addTo(map); |
| 83 | +} |
| 84 | + |
| 85 | +export default element; |
| 86 | +``` |
0 commit comments