Adding Symbols to MapBox GL JS Map
What is Mapbox?
“Mapbox GL JS is a JavaScript library that uses WebGL to render interactive maps from vector tiles and Mapbox styles. It is part of the Mapbox GL ecosystem, which includes Mapbox Mobile, a compatible renderer written in C++ with bindings for desktop and mobile platforms.” — https://docs.mapbox.com/mapbox-gl-js/api/#map
Essentially, Mapbox is a prettier, cheaper, and more accessible alternative to Google Maps.
HTML
Like most libraries or public API’s, the first step is to include the script and style files in your head tags. Then, create an HTML container with the id of “map” in the body.
CSS
This CSS makes the map fill the entire page.
Javascript
The fun part — scripting! This is where the actual magic happens.
Step 1. Copy your access token.
Step 2. Set mapboxgl.accessToken to your access token.
Step 3. Slap that map on the DOM
Get Alessandra Vertrees’s stories in your inbox
Join Medium for free to get updates from this writer.
The basic code for this is: new Map(options: Object)
The ‘Map’ object has an array of properties that you can set upon creation, but the most important one is the “container”. According to the documentation of the “Map” object, the container property is:
“The HTML element in which Mapbox GL JS will render the map, or the element’s string
id. The specified element must have no children.”
-Mapbox GL JS API Documentation
We have successfully slapped the Map object on the DOM! Now we can start manipulating it.
Event Listeners
The ‘Map’ object has what Mapbox refers to as “Instance Members,” which are methods and properties of the map that allow you to dynamically change the map, customize the user experience, and fire events.
map.on is the equivalent to map.addEventListener, but it can take in two or three parameters. The event type and the callback function are the mandatory arguments, but an optional argument is “layerId” and, if used, must be the second argument passed. Example: (‘click’, 1, callBack(e)) or (‘click, callBack(e))
The event object itself is titled “o,” instead of “MouseEvent” or “KeyEvent.” This is what it looks like.
o.lngLat is the longitude and latitude coordinates of the mouse on the map, o.point is the x,y coordinates of the mouse in the window, and originalEvent is where the MouseEvent object is stored.
To create a pin on the map where the user clicked, we need the longitude and latitude coordinates.
Make sure to test your Sh!t in the console to make sure you are referencing the right values :]
Yay!
‘DOM’ Manipulation
in my map.on function I call a function “createPin” that takes in the longitude and latitude coordinates as well as an id. The id generated in the callback function is bad code and was only used as dummy data, please don’t use it.
map.addLayer adds a style layer to the map. It requires a layer object as a parameter. These are the top-level keys for the layer object used in this example. The “id” key is the layer id, which is where we use the id parameter that we are passing into the “createPin” function. The “source” key is an object with many keys, one of them being “features” which is where we set the coordinates as an array.
Fin!









