Welcome back, map maker! In Module One, you learned how to put a basic map on a web page. In Module Two, you learned how to add data sources and layers. In Module Three, you learned how to style your map to make it beautiful and smart.
But there is still something missing. Right now, your map is like a painting. You can look at it, but you cannot talk to it. You cannot click on a school to see its name. You cannot draw a shape on the map. You cannot ask the map to take you to a specific place.
That is where interaction and map controls come in. Interaction means the map responds to what you do. You click, and something happens. You hover, and something appears. Map controls are the buttons and tools that help you use the map. Zoom buttons, a scale bar, a fullscreen button β these are all controls.
In this module, you will learn how to make your map come alive. You will learn how to add click events, popups, and selection tools. You will learn how to add zoom controls, a scale line, and a geolocation button. By the end, your map will not just be a picture. It will be a tool that people can use.
We will keep using simple words and lots of examples. Letβs make your map talk!
After you finish this module, you will be able to:
Emeka lived in Port Harcourt. He loved building things. One day, he made a map of his neighbourhood using OpenLayers. It showed all the shops, schools, and churches. His friends looked at it and said, βThis is nice, but what does each building do? We cannot tell.β
Emeka thought about it. He wanted his map to talk. So he added a special feature. When you click on a shop, a small box appears saying, βThis is Mama Nkechiβs shop. She sells the best puff-puff in town.β When you click on a school, it says, βThis is Sunshine Primary School. It has 500 students.β
His friends were amazed. βIt is like the map is talking to us!β they said. Emeka smiled. He had learned about popups and click events. His map was no longer silent. It was interactive.
Moral of the story: A map that responds to clicks is a map that people love to use.
Definition: Map interaction is when the map responds to something you do. You click, drag, hover, or scroll, and the map reacts.
Why it is important: Interaction makes a map useful. Without it, a map is just a picture.
Simple explanation: Think of a video game. When you press a button, the character moves. That is interaction. A map can do the same.
Real-life example: When you tap a restaurant on Google Maps, a box appears with its name and rating. That is interaction.
School example: A school map where you click a classroom to see the teacherβs name.
Home example: A family map where you click a photo to see a memory.
Nigerian example: A map of Lagos where you click a bus stop to see the next bus time.
Illustration:
You click on a point
|
V
+---------------------+
| Popup appears! |
| "This is a school" |
+---------------------+
Mini summary: Interaction means the map responds to you. Clicking, hovering, and dragging are all interactions.
Definition: A click event is code that runs when you click on the map.
Why it is important: Click events are the foundation of interactivity. They let you respond to the user.
Simple explanation: You tell the map: βWhen someone clicks here, do this.β
Real-life example: When you click a location in a ride-hailing app, it shows the fare.
School example: A school map where clicking a building shows its name.
Home example: A family map where clicking a house shows who lives there.
Nigerian example: A map of Abuja where clicking a government building shows its name.
Example code in OpenLayers:
map.on('click', function(event) {
console.log('You clicked at: ' + event.coordinate);
});
Example code in MapLibre:
map.on('click', function(e) {
console.log('You clicked at: ' + e.lngLat);
});
Illustration:
Click! --> Event fires --> Code runs --> Something happens
Mini summary: Click events let you run code when the user clicks. They are the first step to interactivity.
Definition: A popup is a small box that appears on the map. It usually shows information about a place.
Why it is important: Popups give details without cluttering the map. They appear only when needed.
Simple explanation: When you click a marker, a little speech bubble appears. That is a popup.
Real-life example: When you click a hotel on a travel website, a popup shows the price and rating.
School example: A school map where clicking a classroom shows the subject taught there.
Home example: A family map where clicking a house shows a photo of the family.
Nigerian example: A map of Kano where clicking a market shows what goods are sold there.
Illustration:
β Marker
/
+---------------------+
| Popup |
| "Kano Market" |
| "Sells fabric" |
+---------------------+
Mini summary: Popups are small boxes that show information. They appear when you click.
Definition: In OpenLayers, you create a popup using an Overlay.
Why it is important: Overlays let you place HTML elements on the map.
Simple explanation: You create a div in your HTML. Then you tell OpenLayers to show it at a certain location.
Real-life example: A real estate website shows house details in a popup.
School example: A school map shows room numbers in popups.
Home example: A family map shows recipes in popups.
Nigerian example: A map of Lagos shows traffic reports in popups.
Example code:
var popup = new ol.Overlay({
element: document.getElementById('popup'),
positioning: 'bottom-center'
});
map.addOverlay(popup);
map.on('click', function(event) {
popup.setPosition(event.coordinate);
});
Illustration:
Overlay
|
V
+---------------------+
| HTML div |
| Position: clicked |
+---------------------+
Mini summary: In OpenLayers, popups are made with Overlays. You control the HTML and the position.
Definition: MapLibre has a built-in Popup class. It is very easy to use.
Why it is important: MapLibre popups are simple and look modern.
Simple explanation: You create a popup, set its text, and tell it where to appear.
Real-life example: A weather app shows temperature in a popup.
School example: A school map shows the number of students in a popup.
Home example: A family map shows birthdays in a popup.
Nigerian example: A map of Nigeria shows state capitals in popups.
Example code:
var popup = new maplibregl.Popup()
.setLngLat([8.6753, 9.0820])
.setHTML('<h3>Abuja</h3><p>Capital of Nigeria</p>')
.addTo(map);
Illustration:
new Popup()
|
V
setLngLat()
|
V
setHTML()
|
V
addTo(map)
Mini summary: MapLibre has a simple Popup class. You set the location and the HTML.
Definition: Select is an OpenLayers interaction that lets you click on a feature to select it.
Why it is important: Selection lets you highlight features and show their details.
Simple explanation: When you click a point, it changes colour to show it is selected.
Real-life example: In a map of houses, you click a house and it highlights in yellow.
School example: A school map where you click a classroom and it lights up.
Home example: A family map where you click a house and it shows a photo.
Nigerian example: A map of Lagos where you click a bus stop and it shows the route.
Example code:
var select = new ol.interaction.Select();
map.addInteraction(select);
select.on('select', function(e) {
console.log('Selected: ' + e.selected.length);
});
Illustration:
Click a point
|
V
+---------------------+
| Point highlights |
| Selected! |
+---------------------+
Mini summary: The Select interaction lets you click and highlight features. It is built into OpenLayers.
Definition: Controls are buttons and tools that help you use the map. Zoom buttons, scale bars, and fullscreen buttons are controls.
Why it is important: Controls make the map easier to use. They are like the buttons on a TV remote.
Simple explanation: You add controls to the map, and they appear in a corner.
Real-life example: Google Maps has a zoom control in the bottom right corner.
School example: A school map with a scale bar to show distance.
Home example: A family map with a fullscreen button to see more.
Nigerian example: A map of Nigeria with a geolocation button to find your position.
Example code in OpenLayers:
map.addControl(new ol.control.Zoom()); map.addControl(new ol.control.ScaleLine()); map.addControl(new ol.control.FullScreen());
Example code in MapLibre:
map.addControl(new maplibregl.NavigationControl()); map.addControl(new maplibregl.ScaleControl()); map.addControl(new maplibregl.FullscreenControl());
Illustration:
+---------------------+ | Map | | [+] Zoom | | [β] Zoom | | [βΆ] Fullscreen | | [π] Scale | +---------------------+
Mini summary: Controls are buttons that help you use the map. You can add them easily.
Definition: Geolocation is finding where the user is on the map.
Why it is important: It helps users see where they are and get directions.
Simple explanation: You click a button, and the map shows a blue dot where you are.
Real-life example: When you open a maps app, it shows your location.
School example: A school map that shows where you are standing.
Home example: A family map that shows where each family member is.
Nigerian example: A map of Lagos that shows your position in traffic.
Example code in OpenLayers:
map.addControl(new ol.control.Geolocation({
tracking: true,
projection: 'EPSG:4326'
}));
Example code in MapLibre:
map.addControl(new maplibregl.GeolocateControl({
positionOptions: { enableHighAccuracy: true },
trackUserLocation: true
}));
Illustration:
Click geolocation button
|
V
+---------------------+
| Blue dot appears |
| "You are here" |
+---------------------+
Mini summary: Geolocation finds the userβs location. It is a very useful control.
Definition: Draw is an OpenLayers interaction that lets you draw points, lines, and polygons on the map.
Why it is important: Drawing lets users add their own data. It is great for planning and marking areas.
Simple explanation: You click on the map to start drawing. You click again to finish.
Real-life example: A farmer draws the boundary of his farm on a map.
School example: A student draws the route from home to school.
Home example: You draw a plan of your dream house.
Nigerian example: A town planner draws the boundary of a new market.
Example code:
var draw = new ol.interaction.Draw({
source: vectorSource,
type: 'Polygon'
});
map.addInteraction(draw);
Illustration:
Click -> Click -> Click -> Double-click
| | | |
V V V V
Start Line Line Polygon
Mini summary: The Draw interaction lets you draw shapes on the map. You can draw points, lines, and polygons.
Definition: Modify is an OpenLayers interaction that lets you change the shape of a drawn feature.
Why it is important: Sometimes you make a mistake. Modify lets you fix it.
Simple explanation: You click a shape, and small dots appear at its corners. You drag a dot to change the shape.
Real-life example: A farmer adjusts the boundary of his farm.
School example: A student changes the route of a planned trip.
Home example: You change the shape of your dream house plan.
Nigerian example: A planner adjusts the boundary of a new road.
Example code:
var modify = new ol.interaction.Modify({
source: vectorSource
});
map.addInteraction(modify);
Illustration:
Original shape: Modified shape: +--------+ +----------+ | | | | | | ---> | | | | | | +--------+ +----------+
Mini summary: The Modify interaction lets you change shapes. You drag the corner points.
Definition: A hover effect happens when you move the mouse over something without clicking.
Why it is important: Hover effects give feedback. They tell you that something is clickable.
Simple explanation: When you move your mouse over a button, it changes colour. That is a hover effect.
Real-life example: On a website, a link changes colour when you hover over it.
School example: A school map where hovering over a classroom shows a tooltip.
Home example: A family map where hovering over a photo shows a caption.
Nigerian example: A map of Nigeria where hovering over a state shows its name.
Example code in OpenLayers:
map.on('pointermove', function(event) {
var feature = map.forEachFeatureAtPixel(event.pixel, function(f) {
return f;
});
if (feature) {
console.log('Hovering over: ' + feature.get('name'));
}
});
Illustration:
Mouse moves over point
|
V
+---------------------+
| Tooltip appears |
| "School" |
+---------------------+
Mini summary: Hover effects happen when you move the mouse. They give feedback without clicking.
Definition: A scale line shows the distance on the map. Attribution gives credit to the data source.
Why it is important: The scale line helps you understand distance. Attribution is honest and respectful.
Simple explanation: The scale line is like a ruler on the map. Attribution is a small text that says where the data came from.
Real-life example: A hiking map shows a scale line so you know how far you will walk.
School example: A school map with a scale line to show the length of the football field.
Home example: A family map with a scale line to show the distance to the market.
Nigerian example: A map of Lagos with a scale line to show the distance from Ikeja to Victoria Island.
Example code in OpenLayers:
map.addControl(new ol.control.ScaleLine());
map.addControl(new ol.control.Attribution({
collapsible: false
}));
Illustration:
+---------------------+ | Map | | [====] 10 km | | Β© OpenStreetMap | +---------------------+
Mini summary: The scale line shows distance. Attribution gives credit. Both are important.
Definition: Touch interactions are how you use a map on a phone or tablet. You tap, pinch, and swipe.
Why it is important: Many people use maps on their phones. Your map must work on touch screens.
Simple explanation: Instead of clicking, you tap. Instead of scrolling, you swipe. Instead of zooming with buttons, you pinch.
Real-life example: When you use Google Maps on your phone, you pinch to zoom.
School example: A student uses a tablet to explore a school map.
Home example: Your parents use their phones to find directions.
Nigerian example: A taxi driver uses a phone map to navigate Lagos traffic.
Illustration:
Tap: π (click) Pinch: π€ (zoom) Swipe: π (pan) Double-tap: ππ (zoom in)
Mini summary: Touch interactions are important for phones. Tap, pinch, and swipe.
Definition: You can combine many interactions on one map.
Why it is important: Real maps use many interactions together.
Simple explanation: You can click to select, hover to see names, draw new shapes, and modify them. All on one map.
Real-life example: A city planning tool lets you select buildings, draw new roads, and modify boundaries.
School example: A school map where you can click classrooms, hover for teacher names, and draw new paths.
Home example: A family map where you can click houses, hover for names, and draw routes.
Nigerian example: A map of Abuja where you can select government buildings, hover for names, and draw new bus routes.
Illustration:
+---------------------+ | Interactions: | | [π] Click | | [π±οΈ] Hover | | [βοΈ] Draw | | [βοΈ] Modify | +---------------------+
Mini summary: Combining interactions makes your map powerful. Users can do many things.
Definition: Testing means trying your map to make sure it works.
Why it is important: You do not want your users to find bugs. You want to find them first.
Simple explanation: Click everything. Hover over everything. Try to break it. Then fix it.
Real-life example: Before a new app is released, testers try every button.
School example: Before a school play, students practise the whole play.
Home example: Before you serve food, you taste it.
Nigerian example: Before a new bus route starts, the drivers test the road.
Illustration:
Build map
|
V
Test click
|
V
Test hover
|
V
Test draw
|
V
Fix bugs
|
V
Celebrate π
Mini summary: Always test your map. Click, hover, draw, and modify. Fix any problems.
| Word | Simple Definition |
|---|---|
| Interaction | When the map responds to you. |
| Click event | Code that runs when you click. |
| Popup | A small box with information. |
| Overlay | An HTML element placed on the map. |
| Select | An interaction that highlights a feature. |
| Control | A button or tool on the map. |
| Geolocation | Finding the userβs location. |
| Draw | An interaction to draw shapes. |
| Modify | An interaction to change shapes. |
| Hover | Moving the mouse over something. |
| Scale line | A ruler on the map. |
| Attribution | Credit to the data source. |
div in your HTML with an id (e.g., βpopupβ).new ol.Overlay({ element: document.getElementById('popup') }).map.addOverlay(popup).map.on('click', function(event) { ... }).popup.setPosition(event.coordinate).document.getElementById('popup').innerHTML = 'Hello!'.new maplibregl.NavigationControl().map.addControl(control).map.addControl(control, 'top-right').
Start
|
V
Add click event
|
V
Create popup
|
V
Add controls
|
V
Add draw interaction
|
V
Test everything
|
V
Celebrate π
1993 - First interactive web map 2005 - Google Maps draggable 2006 - OpenLayers released 2007 - iPhone touch maps 2010 - Geolocation on maps 2015 - Vector tiles enable smooth interaction 2020 - MapLibre 2024 - You build interactive maps!
| Control | OpenLayers | MapLibre |
|---|---|---|
| Zoom | ol.control.Zoom | NavigationControl |
| Scale | ol.control.ScaleLine | ScaleControl |
| Fullscreen | ol.control.FullScreen | FullscreenControl |
| Geolocation | ol.control.Geolocation | GeolocateControl |
| Attribution | ol.control.Attribution | AttributionControl |
| Interaction | OpenLayers | MapLibre |
|---|---|---|
| Click | map.on('click') | map.on('click') |
| Hover | map.on('pointermove') | map.on('mousemove') |
| Select | ol.interaction.Select | Custom (via click) |
| Draw | ol.interaction.Draw | Custom (via plugin) |
| Modify | ol.interaction.Modify | Custom (via plugin) |
Lesson 1: Interaction means the map responds to you.
Lesson 2: Click events run code when you click.
Lesson 3: Popups are small boxes with information.
Lesson 4: In OpenLayers, popups use Overlays.
Lesson 5: In MapLibre, popups use the Popup class.
Lesson 6: Select highlights features when clicked.
Lesson 7: Controls are buttons like zoom and scale.
Lesson 8: Geolocation finds the userβs location.
Lesson 9: Draw lets you draw points, lines, and polygons.
Lesson 10: Modify lets you change shapes.
Lesson 11: Hover effects give feedback without clicking.
Lesson 12: Scale lines show distance. Attribution gives credit.
Lesson 13: Touch interactions are important for phones.
Lesson 14: Combining interactions makes a powerful map.
Lesson 15: Always test your map before sharing it.
In this module, you learned how to make your map interactive. You discovered click events and popups. You learned how to use the Select interaction, add controls like zoom and scale, and use geolocation. You explored the Draw and Modify interactions, which let users add and change shapes. You also learned about hover effects, touch interactions, and the importance of testing. Your map is no longer a silent picture. It is a tool that responds to the user. You are now a map builder!
Match the word to its meaning.
| Word | Meaning |
|---|---|
| 1. Popup | A. Finding the userβs location |
| 2. Geolocation | B. A small box with information |
| 3. Draw | C. A button or tool on the map |
| 4. Control | D. Drawing shapes on the map |
| 5. Hover | E. Moving the mouse over something |
Answers: 1-B, 2-A, 3-D, 4-C, 5-E
Title: Build an Interactive Map of Your School
Instructions: In groups of 3, create a map of your school with at least five points (classrooms, library, cafeteria, field, office). Add popups that show what each place is. Add a zoom control and a scale line. Present to the class.
Title: My First Popup
Instructions: Create a map with one point. Add a popup that shows your name and your favourite place. Save and show your family.
Title: A Fully Interactive Map of My Neighbourhood
Instructions: Create an HTML file with a map. Add at least five points of interest (home, school, shop, church, park). Add popups with details for each. Add a zoom control, a scale line, and a geolocation button. Add a draw interaction so users can draw a route. Test on a phone and a computer. Present to the class.
Task: Write a short paragraph (5 sentences) explaining what you learned about map interaction and controls. Then, list three things you found easy and one thing you found hard.
In Module Five, we will learn about project integration and deployment. We will put our maps into real websites using React or Vue. We will learn how to build and deploy our maps so the whole world can see them. To get ready, make sure you:
See you in Module Five! π
End of Module Four Β· Certified OpenLayers/MapLibre User