Class: Map

cercaliagl.Map


Javascript ES5

const map = new cercaliagl.Map({ ... });

The map is the main API component. You can use it for painting markers, features, etc...
This class is an implementation of maplibre.Map (https://maplibre.org/)
For access to MapboxGL Map object, use the getMapboxglMap() method.
To enable 3D landmarks add the parameter &plugin=threebox to the URL

This is the main class of Cercali API Javascript. There are many methods such as create/delete markers or features, handle events interaction with map such as zoom in, zoom out, center, etc.

It also has lot of properties in constructor. Look carefull at the methods and properties.

Example for creating the map:

const map = new cercaliagl.Map({
  target:'divMap',
  controls: []
});

new Map(options)

Name Type Description
options

Map Options

Name Type Description
target string

HTML element ID map container. Mandatory field.

bounds module:cercaliagl/Bounds~Bounds

Initial map bounding box. Default: null.

center module:cercaliagl/LonLat~LonLat

Center map coordinates. Default values x=-2.530985346701069, y=40.14298996438731

controls Array.<string>

Map controls to include. Values:

srcArrow string

Arrow to be used when feature has showDirections = true

disableKeyboardControls boolean

Disable controls to move/rotate/pan map with the keyboard = true

drawStyles Array.<module:cercaliagl/MapStyler~StyleLayerJSON>

Draw styles. Default [].

adaptDrawStylesToFeatureStyle boolean

Adapt draw styles to feature style. Default false.

  • cercaliagl.control.MapControls.SCALELINE
  • cercaliagl.control.MapControls.CLEAN
  • cercaliagl.control.MapControls.GAS_STATIONS
  • cercaliagl.control.MapControls.LAYER_SWITCHER
  • cercaliagl.control.MapControls.METEO
  • cercaliagl.control.MapControls.NAVBAR
  • cercaliagl.control.MapControls.TRAFFIC
  • cercaliagl.control.MapControls.ZOOM
  • cercaliagl.control.MapControls.ISOCHRONES

    If you want a map without controls put value `[]`.
mapLayers Array.<string>

Default map base layers. cercalia.MapTypes.CERCALIA, cercalia.MapTypes.OSM, `cercalia.MapTypes.SATELLITE``.

customBaseLayers Array.<(module:cercaliagl/layer/XYZ~XYZ|module:cercaliagl/layer/WMS~WMS)>

Custom base layers

zoom number

Initial zoom level [0 - 19]. Default value 6

minZoom number

Minimum zoom. Default value 2

maxZoom number

Maximum zoom. Default value 19

cooperativeGestures boolean

Setting cooperativeGesturs to true, scroll to zoom requires command key, and two fingers to pan the map on mobile version.

restrictedBounds module:cercaliagl/Bounds~Bounds

Bounding box limits for map navigation. Default null.

rotation number

Map bearing in degrees. Default value 0

defaultClick function

Assign function to click on map. Function parameters: (1->pixel, 2->cercalia.LonLat).

  1. Pixel where clicked with mouse right click.
  2. Map coordinate.

defaultBaseLayer string

Default base layer. Default cercalia.

lang string

Language, you must put useBrowserLanguage to false. Default en.

useBrowserLanguage boolean

Enable/Disable the usage by default of the browser language. Default true.

geolocationEnabled boolean

Enable automatically geolocation. Your position will be drawn to map.

rightClickMenu boolean

Enable/Disable menu on mouse right click. Default true.

rightClickFunction function

Assign function to mouse right click. For apply this option it's necessary to disable contextmenu on map initialization with option (rightClickMenu:false).
The function parameters are: (1->pixel, 2->cercalia.LonLat).

  1. Pixel where clicked with mouse right click.
  2. Map coordinate.

    Default: `null`.
rightClickMenuOptions Array.<string>

Map rightClick menu options to include. Values:

  • cercalia.ContextMenu.Option.ADDRESS
  • cercalia.ContextMenu.Option.ROUTING
  • cercalia.ContextMenu.Option.DISTANCE

    If you want a map without rightClick menu options put value [].
    Default `[cercalia.ContextMenuOptions.Location, cercalia.ContextMenuOptions.Routing]`.
multipleFeaturesInPixel boolean

Return multiple features in same pixel, for mouse events. Default true.

pitch number

Map pitch. Default value 0

onRotationEnd function

Function is called when the rotation of the map changes (called at the event's end).

onMoveEnd function

Function is called when map is moved (called at the event's end).

onZoomEnd function

Function is called when the zoom of the map changes (called at the event's end).

onPitchEnd function

Function is called when the pitch of the map changes (called at the event's end).

antialias boolean

Antialias for GL render. Default false.

themeColor string

Theme color

routing3d routing3d

Draw 3D polyline for calculated route feature. Default false.

freehandOnDrawPolygonMobile boolean

Draw polygon button on mobile will active freehand mode. Default false.

Methods

module:cercaliagl/Map~Map.animateMarker(marker, feature, speed, numSteps)

Name Type Description
marker Marker

Marker that will be animated

feature Feature

Feature (type linestring) where marker will be animated

speed number

Speed of marker

numSteps number

Number of steps to divide the marker path

addAnimatedFeature(feature, layer, callbackFn)

Name Type Description
feature module:cercaliagl/Feature~Feature

Feature

layer module:cercaliagl/layer/Feature~Feature

Feature layer

callbackFn function

callback function when finished

addBaseLayer(baseLayer, mapType)

Name Type Description
baseLayer module:cercaliagl/layer/XYZ~XYZ | module:cercaliagl/layer/WMS~WMS

Base layer

mapType string | undefined

Map type

addCustomControl(control, position)

Add custom control. You can specify children position inside control container. Default append to last child.

Name Type Description
control module:cercaliagl/control/Custom~Custom

Custom control.

position number

Position.

addFeature(feature, layer, updateGeoJSON)

Name Type Description
feature module:cercaliagl/Feature~Feature

Feature

layer module:cercaliagl/layer/Feature~Feature

Feature layer

updateGeoJSON boolean (defaults to true)

Update GeoJSON after add feature, true by default

addFeatures(features, layer)

Name Type Description
features Array.<module:cercaliagl/Feature~Feature>

Features

layer module:cercaliagl/layer/Feature~Feature

Feature layer

addGroupBaseLayer(id, label, baseLayers, mapType)

Name Type Description
id string

id

label string

label

baseLayers Array.<(module:cercaliagl/layer/XYZ~XYZ|module:cercaliagl/layer/WMS~WMS)>

Base layers

mapType string | undefined

Map type

addLayer(layer, beforeLayerId)

Add layer

Name Type Description
layer module:cercaliagl/layer/Base~LayerLike

Layer

beforeLayerId string

Before layer id. Only when base layer is MVT type.

addMarker(marker, layer)

Name Type Description
marker module:cercaliagl/Marker~Marker

Marker

layer module:cercaliagl/layer/Marker~Marker

addMarkers(marker, layer)

Name Type Description
marker Array.<module:cercaliagl/Marker~Marker>

Marker

layer module:cercaliagl/layer/Marker~Marker

Layer

centerOnGeolocation()

Center on current GPS position

centerToFeatures(features, options)

Name Type Description
features Array.<module:cercaliagl/Feature~Feature>

Features

options

Options

Name Type Description
animation boolean

Animation.

duration number

Duration if animation is enabled. Default 2000.

changeZoom boolean

Change zoom

zoom number

zoom to change

centerToMarkers(markers, options)

Name Type Description
markers Array.<module:cercaliagl/Marker~Marker>

Markers

options

Options

Name Type Description
animation boolean

Animation.

duration number

Duration if animation is enabled. Default 2000.

changeZoom boolean

Change zoom

zoom number

zoom to change

changeBaseLayer(baseLayer)

Available layers.

  • cercaliagl.MapTypes.CERCALIA
  • cercaliagl.MapTypes.SATELLITE
  • cercaliagl.MapTypes.OSM
  • cercaliagl.MapTypes.DARK
  • cercaliagl.MapTypes.SILVER
  • cercaliagl.MapTypes.BLANKMAP
  • cercaliagl.MapTypes.GOOGLE_STYLE
  • cercaliagl.MapTypes.EMPTY
Name Type Description
baseLayer string

Base layer

closeAll()

Close all menus and reset nav bar menu view.

closePopups(layer)

Close all open popups

Name Type Description
layer module:cercaliagl/layer/Marker~Marker | undefined

Returns the pixel value from a coordinate

Name Type Description
lonLat module:cercaliagl/LonLat~LonLat

Coordinate.

Returns:
Return pixel of map.

disableClustering(layerId)

Disable marker clustering. Only available for module:cercaliagl/layer/Marker~Marker layer.

Name Type Description
layerId string

Layer id. Default LayersId.MARKERS_LAYER_ID.

disableDrawInteraction()

Disable draw interaction

disableHeatmap(layerId)

Name Type Description
layerId string

Layer id. Default LayersId.MARKERS_LAYER_ID.

enableClustering(options)

Enable marker clustering. Only available for module:cercaliagl/layer/Marker~Marker layer.

Name Type Description
options

Clustering options

Name Type Description
layerId string

Layer id. Default LayersId.MARKERS_LAYER_ID.

onClick function

Click over callback function.

onMouseOver function

Mouse over callback function.

onMouseOut function

Mouse out callback function.

maxZoom number

Max zoom for clustering. Default 16.

radius number

Radius in pixels to cluster. Default 40.

enableDrawInteraction(type, options)

Enable draw interaction

Name Type Description
type string

Available values line|polygon|draw_magnetic_polygon|circle|freehand_polygon

options

Draw options

Name Type Description
onDrawEnd function

Callback function when feature has been drawn. It receive geometry drawn

enableHeatmap(options)

Converts layer into heatmap. There are two layers: Zone layer for heat, and points layer. Points layers is useful to show markers in the nearest zooms. For better adaptation customize layer styles. Depends of number and density you should adjust heat layer styles. Read MapboxGL documentation to styling both layers: https://docs.mapbox.com/mapbox-gl-js/style-spec/layers/#heatmap.

Default values for styles for zones if options.styleHeatZones property is not provided:

{
  maxzoom: 15,
  paint: {
    'heatmap-intensity': [
      'interpolate', ['linear'], ['zoom'], 0, 0.1, 15, 1.5,
    ],
    'heatmap-color': [
      'interpolate', ['linear'],
      ['heatmap-density'], 0, 'rgba(0, 0, 255, 0)', 0.1, 'royalblue', 0.3, 'cyan', 0.5, 'lime', 0.7, 'yellow', 1, 'red',
    ],
    'heatmap-radius': [
      'interpolate', ['linear'], ['zoom'], 0, 1, 15, 14,
    ],
    'heatmap-opacity': [
      'interpolate', ['linear'], ['zoom'], 7, 0.8, 15, 0.3,
    ],
  },
}

Default values for styles for points if options.styleHeatPoints property is not provided:

{
  minzoom: 14,
  paint: {
    'circle-radius': [
      'interpolate', ['linear'], ['zoom'], 12, 3, 16, 6,
    ],
    'circle-color': 'rgba(0,0,0,0.4)',
    'circle-stroke-color': 'white',
    'circle-stroke-width': 1,
  },
}
Name Type Description
options

Heatmap layer options

Name Type Description
layerId string

Layer id. Default LayersId.MARKERS_LAYER_ID.

styleHeatZones module:cercaliagl/layer/Marker~StyleHeatLayer

Style layer zone. Read MapboxGL documentation https://docs.mapbox.com/mapbox-gl-js/style-spec/layers/#heatmap.

styleHeatPoints module:cercaliagl/layer/Marker~StyleHeatLayer

Point layer zone. Use layer type: circle styles. https://docs.mapbox.com/mapbox-gl-js/style-spec/layers/#circle

exportPNG(filename)

Export PNG image

Name Type Description
filename string (defaults to ExportCercaliaMap.png)

Filename.

Get map center

Returns:
Map center

getContainerId(){string}

Returns the container ID

Returns:
id Map container Id

Get context menu. Useful if user wants to add or remove

Returns:
Context menu

getControlByName(name)

Name Type Description
name string

Name

getFeatureById(id, layer){module:cercaliagl/Feature~Feature|null}

Get marker by ID. It returns null if it is not found.

Name Type Description
id string

Id.

layer module:cercaliagl/layer/Feature~Feature

Marker layer.

Returns:
marker Marker or null if not found.

getFeatures(layer)

Name Type Description
layer module:cercaliagl/layer/Feature~Feature
Returns:
Geolocation

getLang(){string}

Returns:
Language

Get layer by id

Name Type Description
layerId string

Layer id

Returns:
Map routing object
Returns:
Map routing object

getMarkerById(id, layer){module:cercaliagl/Marker~Marker|null}

Get marker by ID. It returns null if it is not found.

Name Type Description
id string

Id.

layer module:cercaliagl/layer/Marker~Marker

Marker layer.

Returns:
marker Marker or null if not found.

getMarkers(layer)

Name Type Description
layer module:cercaliagl/layer/Marker~Marker

getRotation(){number}

Returns:
Rotation in degrees.

getSize(){Array.<number>}

Return width & height container div in pixels

Returns:
Size in pixels

getZoom(){number|undefined}

Returns:
Current zoom level of map.

hideControls()

Hide all map controls

hideLabels(layer)

hide all marker labels

Name Type Description
layer module:cercaliagl/layer/Marker~Marker | undefined

isReady()

off(eventName, callbackFn)

Name Type Description
eventName string

Event name

callbackFn function

Callback function

on(eventName, callbackFn)

Name Type Description
eventName string

Event name

callbackFn function

Callback function

openContextMenu(lonLat)

Open the context menu at the coordinates specified by parameters. If not specified, opens the context menu in the map center according to the current position.

Name Type Description
lonLat module:cercaliagl/LonLat~LonLat

Coordinate

openPopups()

Show all markers popups. Only for markers with popup created

panTo(position, animation)

Moves the map to the new center

Name Type Description
position module:cercaliagl/LonLat~LonLat

Coordinates.

animation boolean (defaults to true)

Enable or disable animation. Default true.

panToBounds(bounds, animation)

Map center maintaining the zoom level

Name Type Description
bounds module:cercaliagl/Bounds~Bounds

Mapbounds.

animation boolean

Enable or disable animation. Default true.

pixelsToMeters(pixels){number|null}

Calculate the real distance in meters, from pixels values.

Name Type Description
pixels number

Pixels to measure

Returns:
meters Return measure in meters.

Returns the coordinate from a pixel value

Name Type Description
pixel module:cercaliagl/pixel~Pixel

pixel of map.

Returns:
Coordinate

removeAllFeatures(layer)

Name Type Description
layer module:cercaliagl/layer/Feature~Feature

Feature layer

removeAllMarkers(layer)

Name Type Description
layer module:cercaliagl/layer/Marker~Marker

removeFeature(feature, layer)

Name Type Description
feature module:cercaliagl/Feature~Feature

Feature

layer module:cercaliagl/layer/Feature~Feature

Feature layer

removeFeatures(feature, layer)

Name Type Description
feature module:cercaliagl/Feature~Feature

Feature

layer module:cercaliagl/layer/Feature~Feature

Feature layer

removeLayer(layer)

Remove layer

Name Type Description
layer module:cercaliagl/layer/Base~LayerLike

Layer

removeMarker(marker, layer)

Name Type Description
marker module:cercaliagl/Marker~Marker

Marker

layer module:cercaliagl/layer/Marker~Marker

Layer

removeMarkers(markers, layer)

Name Type Description
markers Array.<module:cercaliagl/Marker~Marker>
layer module:cercaliagl/layer/Marker~Marker

rotate(angle, animation)

Rotates the map degrees, by parameter

Name Type Description
angle number

Angle (degrees).

animation boolean (defaults to true)

Animation. Default true.

setCenter(lonLat, zoom, animation, speed)

Set the map center

Name Type Description
lonLat module:cercaliagl/LonLat~LonLat

Coordinates

zoom number

Zoom

animation boolean (defaults to false)

Enable or disable animation. Default true.

speed number (defaults to 1)

Speed animation. Default 1.

setContextMenu(contextMenu)

Name Type Description
contextMenu module:cercaliagl/ContextMenu~ContextMenu

Context menu

setDefaultClick(func)

Set default click function. When map is clicked parameter function is triggered.

Name Type Description
func function | undefined

Function

setLanguage(lang)

Name Type Description
lang string

Language

setPitch(pitch, animation)

Name Type Description
pitch number

Pitch

animation boolean (defaults to true)

Animation. Default true.

setRotation(bearing, animation)

Sets the map rotation

Name Type Description
bearing number

Angle in degrees.

animation boolean (defaults to true)

Animation. Default true.

setZoom(zoom)

Set the zoom level of map. If second parameter is specified (boolean, optional)

Name Type Description
zoom number

Zoom level to adjust

showBuildings3D(visible, showToBuildings)

Show 3D buildings in close zooms.

Name Type Description
visible boolean

Visible or not.

showToBuildings boolean (defaults to false)

Buildings

showControls()

Show map controls

showInfoMap(infoMap)

Name Type Description
infoMap module:cercaliagl/InfoMap~InfoMap

Info map.

showLabels(layer)

show all marker labels

Name Type Description
layer module:cercaliagl/layer/Marker~Marker | undefined

toggleFullScreen()

Toggle full screen, only available inside any button event.

updateSize()

Force a recalculation of the map viewport size. This should be called when map container changes its size