Create a map
To be able to start with Mapbox, you need to create an account, otherwise you won't be able to show a map. You can do it on this page. Once your account created and activated, you'll have access to your Dashboard, which contains a particular, very interesting piece of data:
Your access token (it should begin with pk.
)
This access token is a key, which is necessary to be able to identify you when you'll want to show a map. You need to be identified for two main reasons:
- It allows to link the map to your account, taking into account any change you did on maps of your Mapbox account. You can create custom styles for instance, thanks to the studio, and those styles wouldn't be found if you would use someone else's access token.
- It also allows to count the number of map loads (or map displays) you asked for, which is necessary to have account and payment statistics.
Mapbox offers 50 000 map loads per month, which is enough in a lot of cases. However, if you're developing a website that should quickly have more than 50K views (on the page where the map is loaded), you will have to pay $5 per 1000 views (beyond the 50K free views).
You also need to know that there are alternatives to Mapbox, with lower fees, but it doesn't exclude developing the map with Mapbox tools (you can use Mapbox open source tools but use other map displaying apis).
Let's do this, let's show our first map !
I will explain how to do this in different steps, but don't worry, you'll find the final code at the end of this blog post.
To start with, open the file containing the HTML code of your page, or, on WordPress, edit the page that should show the map, by adding a new Custom HTML bloc, and add this content:
<script src="https://api.mapbox.com/mapbox-gl-js/v2.0.1/mapbox-gl.js"></script>
<link href="https://api.mapbox.com/mapbox-gl-js/v2.0.1/mapbox-gl.css" rel="stylesheet" />
<div id="map" style="height: 500px; width: 100%"></div>
<script>
mapboxgl.accessToken = 'Dont forget to replace this by your access token';
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/light-v10',
center: [2, 45],
zoom: 2
});
</script>
Save, show the draft of your page and ... Eureka! 🎉
Now you should see the map displayed.
Your first map on a website or WordPress blog with Mapbox
Let's split the code before in different pieces:
- The two first lines are used to load the necessary resources to make the map work as expected:
<script src="https://api.mapbox.com/mapbox-gl-js/v2.0.1/mapbox-gl.js"></script>
<link href="https://api.mapbox.com/mapbox-gl-js/v2.0.1/mapbox-gl.css" rel="stylesheet" />
Please note that the placement of those lines has an impact on the page speed. Ideally, they should be placed at the end of your site's <body>
tag. - A single line is needed to place the map on the page, and it also defines the map container's style (500px height and 100% of the width):
<div id="map" style="height: 500px; width: 100%"></div>
- Finally, the script with let us to execute the code that allows us to load the map and show it:
<script>
mapboxgl.accessToken = 'Dont forget to replace this by your access token';
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/light-v10',
center: [2, 45],
zoom: 2
});
</script>
Don't forget to add your access token to this piece of code, otherwise, the map won't load.
Each parameter passed to the mapboxgl.Map
initialization can be changed:
- container: It should be the same as the id of the container's element (see above)
- style: it's the map style. You can customize them or create new ones on the Mapbox Studio. Mapbox has 4 or 5 presets to help you get started quickly. You can visualize them here.
- center: this is the map's default center. The first figure
(2)
corresponds to the longitude, and the second one (45)
corresponds to the latitude. - zoom: this is the default zoom, it should be a number between 0, which is the largest scale, the more dezoomed, and 20, which is the smallest scale, the more zoomed.
With a zoom level equals to 20, you'll see the streets and homes. The best zoom level for a use case like showing worldwide trips is between 1 and 3.