Published on 25 November 2026
Integrating swisstopo Vector Tiles with External APIs
This tutorial is intended for web developers and advanced users who are familiar with JavaScript, MapLibre GL JS and basic web mapping concepts. You should understand how vector tiles and map styles work. Previous experience with APIs is helpful, but the relevant concepts will be explained throughout this tutorial.
Introduction
Vector tiles provide a flexible way to display geographic information on interactive maps. In this tutorial, we will use MapLibre GL JS together with the swisstopo vector tile basemap to build an interactive map of public transport stops in Switzerland.
By accessing information embedded in the vector tiles and combining it with an external API, we can turn a standard basemap into an interactive application. When users click on a public transport icon, the map retrieves and displays the next scheduled departures for that stop using the Swiss Transport API.
The tutorial will guide you through the process step by step, starting with a basic swisstopo basemap and gradually adding the functionality needed to identify transport stops, retrieve departure information and display it in a popup. By the end, you will have learned how to:
- Display a swisstopo vector tile basemap using MapLibre GL JS.
- Identify public transport points of interest (POIs) in the vector tiles.
- Access attributes stored in vector tile features.
- Retrieve public transport departure information from an external API.
- Display the retrieved information in an interactive map popup.
- Highlight the selected transport stop on the map.
Try the interactive map
Click on any public transport icon to see the next departures for that stop.
We will begin by creating a standalone HTML document that displays a swisstopo basemap using MapLibre GL JS. First, create a new file named index.html and add the following code:
Save the file and open it in a web browser. You should now see the swisstopo basemap centered on Bern.
Before connecting the public transport stops to an external API, we first need to understand where the relevant information is stored and how the stops are represented on the map. The swisstopo vector tiles separate the underlying geographic data from its visual representation. We will explore these two aspects using the official vector tile schema documentation and Maputnik.
2.1 Explore the vector tile schema
Open the swisstopo Vector Tile Schema documentation
The documentation describes the structure of the vector tile data, including the available source layers, their attributes and the types of features they contain. For this tutorial, we are particularly interested in the poi source layer. It contains points of interest, including public transport stops. Locate the poi layer in the documentation and examine its available attributes. Pay particular attention to:- subclass: describes the specific type of point of interest. We will use this attribute to distinguish railway stations, tram stops and bus stops.
- station_id: provides the identifier associated with a public transport stop. We will need this value to request departure information from the Swiss Transport API.
These attributes are part of the underlying vector tile features. They are not simply labels or visual properties: they provide information that we can access programmatically when users interact with the map. The relevant `subclass` values for our application are: railway_station, tram_stop and bus_stop.
2.2 Explore the swisstopo BaseMap style in Maputnik
The schema tells us what information is available in the vector tiles. But how do these features become the icons and symbols we see on the map? This is defined by the map style. To explore it visually, we will use Maputnik, an open-source visual editor for MapLibre styles.
- Open Maputnik.
- Select open in the top menu.
- Choose Load from URL
- Paste the swisstopo basemap style URL and open: https://vectortiles.geo.admin.ch/styles/ch.swisstopo.basemap.vt/style.json
You should now see the swisstopo map in Maputnik, together with its list of rendering layers. For our public transport map, we need to identify the relevant style layers which display public transport points of interest (poi). The swisstopo basemap contains a large number of rendering layers. To find the ones relevant to our application, look for:
- poi_rank1
- poi_rank2
Select each layer in Maputnik and inspect its configuration. In particular, pay attention to the following properties: Source: Identifies the vector tile source used by the rendering layer.
- Source layer: Identifies the underlying data layer within the vector tiles. For our public transport icons, this is poi.
- Filter: Defines which features from the source layer are displayed by the particular rendering layer.
- Layout and Paint: Define how the features are displayed, including their icons, size and other visual properties.
You can inspect the filters to see how the rendering layers distinguish between different types of POIs. In our case, the subclass attribute is used to identify public transport features such as railway stations, tram stops and bus stops.

2.3 Understand the relationship between vector tile data and map style
We have now identified the two essential parts of the map:
It is important to understand that poi_rank1 and poi_rank2 are rendering layers defined in the style, not separate source layers in the vector tiles. Both use the underlying poi data. This distinction is central to our application: we will use the style layer information to identify which map features users can interact with, and the attributes of the underlying vector tile features to retrieve the corresponding public transport information.
In the next step, we will connect these two components in MapLibre GL JS and use the station_id attribute to request departure information from the Swiss Transport API.So far, we have created a map displaying the swisstopo basemap and explored how public transport stops are represented in its vector tiles. The next step is to make these features interactive. When a user clicks on a public transport icon, we want to retrieve information about the corresponding stop and use it to request departure information from the Swiss Transport API. However, clicking an icon on the map does not automatically provide us with the information required by the API. We first need to establish a connection between the clicked map feature and its underlying vector tile attributes.
3.1 Retrieve feature information at the clicked position
MapLibre GL JS provides a method called queryRenderedFeatures(). It allows us to retrieve vector tile features that are currently rendered at a specific screen position. This is particularly useful for our application: instead of creating a separate dataset of clickable transport stops, we can directly interact with the features already displayed by the swisstopo basemap. When a user clicks on the map, we query the relevant rendering layers and inspect the returned features. We can then access their attributes, including the station_id that we identified in Step 2.
Add the following event handler inside the existing3.2 Understand how the interaction works
The event handler connects three elements: the user's click, the rendered vector tile feature and its attributes.
Capture the map click
This registers a click event on the map. Whenever a user clicks somewhere on the map, the associated function is executed. The event object contains information about the interaction, including the screen position of the click.
Retrieve the rendered features
The queryRenderedFeatures() method retrieves the features rendered at the clicked position. By specifying the two style layers, we restrict the query to the public transport-related rendering layers identified earlier. The result is an array of features. Each feature contains information about its source, rendering layer, geometry and attributes.
Select the relevant feature
The `find()` method searches the returned features for the first one matching our criteria. We use the layer `ID` and the `subclass` attribute to distinguish the railway stations, tram stops and bus stops that are relevant to our application.
Extract the station identifier
Once we have identified the feature, we can access its attributes through the properties object. Here, we retrieve the station_id value. This is the essential link between the map and the external API: the station identifier allows us to request information about the selected public transport stop.
3.3 Test the interaction
Save the HTML file, reload the map and open your browser's developer console (press F12, once the browser is opened). Click on a few different public transport icons. For each selected stop, the console should display its station identifier, for example: Selected station ID: 8579896. The actual identifier will depend on the stop you select.
If you click somewhere else on the map, the code will report that no public transport stop was selected.
At this point, we have successfully connected the rendered public transport features with their underlying vector tile attributes. We can now use the extracted station_id to retrieve information about the selected stop.
In this chapter, we will extend the existing map with two additional functions:
- Highlight the selected public transport stop on the map.
- Retrieve the next departures for that stop using the Swiss Transport API.
4.1 Add a highlight for the selected stop
The GeoJSON source is initially empty. It will be updated whenever a user selects a public transport stop. The selected-stop-highlight layer displays the selected feature as a translucent blue circle with a white outline. Importantly, this is an additional layer. The existing swisstopo POI layers remain unchanged, so their original icons and styling are preserved.
4.2 Understand the API request
We will use the /stationboard endpoint of the Swiss Transport API. This endpoint returns upcoming departures for a specific public transport stop. It accepts the station identifier as the id parameter and allows us to specify how many departures we want to retrieve using the limit parameter.
We construct the request URL using the selected station's identifier. The limit=3 parameter asks the API to return the next three departures.
The API returns its response in JSON format. Among other information, it includes the station name and a list of departures, with details such as the line number, destination, platform, departure time and delay.The response contains two main elements relevant to our application:
- data.station contains information about the selected station, including its name.
- data.stationboard contains an array of upcoming departures.
Each departure contains information such as:
4.3 Extend the click handler
We will now combine the map interaction, the selection highlight and the API request in one click handler.
- In your existing HTML file, locate the map.on("click", ...) event handler.
- Replace the entire existing click handler with the following code:
Notice that we use queryRenderedFeatures(event.point) without restricting the query to specific layers. We then identify the relevant features using their rendering layer IDs and subclass attributes. This follows the approach used in our working example map.
4.4 Understand the updated click handler
We first use The queryRenderedFeatures() method to retrieve the features rendered at the clicked screen position. The find() method searches the returned features for a railway station, tram stop or bus stop. We use the rendering layer ID and the subclass attribute to identify the relevant feature.
We highlight the selected stop with the `setData()` method which updates the GeoJSON source with the geometry of the selected feature. The highlight layer automatically displays the new geometry. If the user clicks somewhere without a relevant public transport stop, the source is cleared and the previous highlight disappears.
Then we retrieve the departure information:
As seen in chapter 4.2, We construct the request URL using the selected station's identifier. The limit=3 parameter asks the API to return the next three departures. The fetch() function sends the HTTP request, while await pauses execution until the response is received. We then convert the JSON response into a JavaScript object.
4.5 Test the API Integration
- Save the HTML file, reload the map and open the browser's developer console (F12).
- Click on a public transport icon. If everything works, the console should display the selected station and its next departures.
- Try clicking on a few different stops to see how the returned information changes.

At this point, our map can retrieve departure information dynamically whenever a user selects a public transport stop. In the next step, we will present this information directly on the map using a MapLibre popup instead of displaying it in the developer console.
In the previous chapter, we connected the map to the Swiss Transport API and retrieved departure information when a public transport stop was selected. So far, the results have only been displayed in the browser console. In this chapter, we will display the departure information directly on the map using a popup. We will build on the existing map and selected-stop highlight, adding a small instruction overlay and updating the click handler to show the next three departures.
5.1 Add the popup and map instruction styles
Add the following CSS to the existing
Next, update the existing map container in the HTML body to include the instruction. Keep a single element with id="map":
5.2 Update the click handler
We can now replace the click handler from Chapter 4 with an updated version that displays the departure information in a popup. The handler still identifies the selected public transport stop and updates the map highlight. It then creates a popup, fetches the next three departures from the Swiss Transport API, and displays the results. Replace the existing map.on("click", ...) handler with the following code:
Save index.html and reload the map in your browser. Click on a railway station, tram stop, or bus stop. You should see the selected stop highlighted on the map and a popup showing its name and the next three departures, including the line, destination, platform, departure time, and delay information when available. If the API returns no departures, the popup displays a message. If the request fails, it displays an error message instead.
The combination of swisstopo vector tiles and external APIs opens up many possibilities for interactive web applications. The same approach used in this tutorial can be applied to other use cases, for example:
- Tourism: Enrich points of interest with descriptions, images, opening hours or visitor information from external data sources.
- Real-time information: Display live information such as public transport departures, traffic conditions or weather data.
- Environmental data: Link geographic features with measurement data, such as weather stations, water levels or air quality information.
- Public services: Add information about public facilities, parking spaces or other local services.
- Geospatial statistics: Display statistical data by linking data to spatial units.
The key idea is to use vector tiles not only as a background map, but also as a source of geographic features and attributes that can be linked to other data. This allows you to build applications that go far beyond displaying a map.
