Js 10

  • uno

  • due

  • non

Mapbox Map parameters and methods

The mapboxgl Map Object can be used to:

Starting from a basic map:

new mapboxgl.Map({
      container: "map", 
      style: "mapbox://styles/mapbox/streets-v11", 
      center: [12, 42, 
      zoom: 9, 
});

We can include:

new mapboxgl.Map({
  ...
  antialias: true,    
  bearing: 20,          //initial degree rotation
  bearingSnap: 45,      //max degree rotation before map snap to normal
  clickTolerance: 20,   //the pixels that need to be swept to get a drag before being a click
  cooperativeGestures: true,  //the zoom will need the crtl button but doesn't stops double click to zoom
  doubleClickZoom: false,     //to disable double-click zoom
  dragPan: false,       //will disable the drag move map
  dragRotate: false,    //won't allow rotate nor pitch 3D
  interactive: false,   //will disable any movement or zoom
  maxZoom, minZoom, maxPitch, minPitch:   //for limits (max) or immediate(min)
  pitchWithRotate: false      //will disable pitch 
})

We use projection to change the view of the map:

Cover

projection: "mercator" will create a continous map

Cover

projection: "albers" will give us a conic projection

Cover

projection: "equalEarth" will have rounded border

The map event listeners can include Layers

chevron-rightStarting sourcehashtag

We can have:

First for the mouse movements in/out the map:

We also have event listeners for when the map is dragged/moved:

For the Event to trigger on the Layer elements we:

We can then use its event features[0].properties:

Querying features of Mapbox layers

We can use queryRenderedFeatures() to get an array of GeoJSON Feature objects from the layer we are on, using visible features that satisfy the query parameters (geometry):

chevron-rightFilter feature properties of a clicked query Pointhashtag
On click() we filter the query specific features

We can include the Query Features in the event listener:

Coordinates and Pont position

We can also change the cursor if in a Layer using getCanvas():

An example with query features and layers:

Hover on Layer event
chevron-rightPaint layers and Hover query feature eventshashtag

On load we:

We can also mix queryRenderedFeatures() and Layers:

we select the Layer features objects by the query
chevron-rightHow to draw expandible square on canvas mapboxglhashtag

First, we need the CSS for the box:

Then we need the Canvas :

We are gonna use the [start, mousePos(e)] next

For the Souce and filtered layers we:

Now that we got a query features area we:

Instead of box on drag we use 2 click boxes
chevron-rightCustom 2 points box for query feature eventshashtag

We will need 2 arrays, one for the markers on coordinates and another for the canvas points:

QuerySourceFeatures() returns an array of GeoJSON Feature objects from its source that satisfy the query parameter:

Instead of using an area in Features we use the source to filter
chevron-rightFilter features containing using its datahashtag

The source is similar but we use a different property:

We use querySourceFeatures() on the layer:

We use set() to not repeat features on getUniqueFeatures():

We can also create a 3D terrain map using mapbox-dem:

we can also exagerate the heigths

Last updated