Are you using Google Maps and want to switch to Mapbox? You’ve come to the right place! This tutorial shows you how to use the Mapbox GL JS JavaScript library to create a web map, add a marker to the map, and attach a popup to the marker using methods like those that the Google Maps JavaScript API uses.
This guide assumes that you are already familiar with the Google Maps JavaScript API V3 and with front-end web development concepts including HTML, CSS, and JavaScript.
To complete this tutorial, you will need:
index.html
.<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Switch to Mapbox</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
{/* Import Mapbox GL JS */}
<script src="https://api.tiles.mapbox.com/mapbox-gl-js/v3.3.0/mapbox-gl.js"></script>
<link
href="https://api.tiles.mapbox.com/mapbox-gl-js/v3.3.0/mapbox-gl.css"
rel="stylesheet"
/>
<style>
body {
margin: 0;
padding: 0;
}
#map {
position: absolute;
top: 0;
bottom: 0;
width: 100%;
}
</style>
</head>
<body>
{/* Create a container for the map. */}
<div id="map"></div>
<script>
// Add your Mapbox access token.
mapboxgl.accessToken = 'YOUR_MAPBOX_ACCESS_TOKEN ';
// Code from the next step will go here.
</script>
</body>
</html>
This code imports the Mapbox GL JS JavaScript library and CSS file so your map can use Mapbox GL JS functionality and styles. It also creates a <div>
element with an id
of 'map'
that will be the map's container.
Find mapboxgl.accessToken
in the code and set its value to your own Mapbox access token, as described in Getting started. Using your access token associates your new map with your Mapbox account.
Save your HTML file.
In this step, you will initialize a map on your webpage.
With the Google Maps JavaScript API, you initialize a new map like this:
const map = new google.maps.Map(document.getElementById('map'), {
mapTypeId: 'roadmap',
center: { lat: 64.1436456, lng: -21.9270884 },
zoom: 13
});
With Mapbox GL JS, you can initialize a map in a similar way.
index.html
file inside the <script>
tags, after mapboxgl.accessToken
:const map = new mapboxgl.Map({
container: 'map', // HTML container ID
style: 'mapbox://styles/mapbox/streets-v12', // style URL
center: [-21.9270884, 64.1436456], // starting position [lng, lat]
zoom: 13 // starting zoom
});
The code above creates and initializes a new map:
container
option specifies an HTML element to contain the map.style
option uses the style URL for Mapbox Streets to set the style
property for the map. You can use a custom style created with Mapbox Studio, or you can change your map's style dynamically in the browser at runtime, depending on your needs.center
and zoom
properties specify the map's starting position and zoom level.Now you're ready to add a single marker to your map.
With the Google Maps JavaScript API, you can add a marker to a map as shown below:
const map = new google.maps.Map(document.getElementById('map'), {
mapTypeId: 'roadmap',
center: { lat: 64.1436456, lng: -21.9270884 },
zoom: 13
});
const marker = new google.maps.Marker({
position: { lat: 64.1436456, lng: -21.9270884 },
title: 'Reykjavik Roasters - Coffee Shop',
map: map
});
<script>
tag:const marker = new mapboxgl.Marker()
.setLngLat([-21.9270884, 64.1436456])
.addTo(map);
This code uses the default Marker
method to create a new marker:
setLngLat()
method to specify coordinates for where the marker will appear.addTo()
method to add the marker to the map.There are many different ways to add markers to a Mapbox GL JS map. For example, you can also attach markers to a set of points by loading a GeoJSON source or a vector tileset source. See our Add markers getting started guide for more information on adding markers to a map.
Now you will add a popup that displays information about a location when a user clicks on a marker.
In the Google Maps JavaScript API, an interactive popup is called an InfoWindow
and you add one like this:
const map = new google.maps.Map(document.getElementById('map'), {
mapTypeId: 'roadmap',
center: { lat: 64.14356426, lng: -21.92661562 },
zoom: 13
});
const marker = new google.maps.Marker({
position: { lat: 64.14356426, lng: -21.92661562 },
map: map
});
const infowindow = new google.maps.InfoWindow({
content: `<h3>Reykjavik Roasters</h3><p>A good coffee shop</p>`
});
marker.addListener('click', () => {
infowindow.open(map, marker);
});
In Mapbox GL JS, you can attach a Popup
directly to the marker and it will automatically appear when the marker is clicked. You do not need to add an event listener.
marker
declaration with this new one.const popup = new mapboxgl.Popup().setHTML(
`<h3>Reykjavik Roasters</h3><p>A good coffee shop</p>`
);
const marker = new mapboxgl.Marker()
.setLngLat([-21.92661562, 64.14356426])
.setPopup(popup)
.addTo(map);
Popup
object populates it with some HTML content about a coffee shop..setPopup()
to the marker
declaration to attach the popup to the marker.setHTML()
.<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Demo: Switch from Google Maps to Mapbox</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<script src="https://api.tiles.mapbox.com/mapbox-gl-js/v3.3.0/mapbox-gl.js"></script>
<link
href="https://api.tiles.mapbox.com/mapbox-gl-js/v3.3.0/mapbox-gl.css"
rel="stylesheet"
/>
<style>
body {
margin: 0;
padding: 0;
}
#map {
position: absolute;
top: 0;
bottom: 0;
width: 100%;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
mapboxgl.accessToken = '{{MAPBOX_ACCESS_TOKEN}}';
const map = new mapboxgl.Map({
container: 'map', // HTML container id
style: 'mapbox://styles/mapbox/streets-v12', // style URL
center: [-21.92661562, 64.14356426], // starting position as [lng, lat]
zoom: 13
});
const popup = new mapboxgl.Popup().setHTML(
`<h3>Reykjavik Roasters</h3><p>A good coffee shop</p>`
);
const marker = new mapboxgl.Marker()
.setLngLat([-21.92661562, 64.14356426])
.setPopup(popup)
.addTo(map);
</script>
</body>
</html>
Congratulations! You have used Mapbox GL JS to make a web map with a marker and a popup!
Now that you have switched from Google Maps to Mapbox and made your first map with Mapbox GL JS, explore our other Mapbox GL JS tutorials for more ways to build on your map:
Learn more by checking out the Mapbox GL JS documentation and examples!