# Common Map Methods

# Initialize Map

Use the vjmap.Map object to initialize a map.

// Create a new map service object with service URL and token
let svc = new vjmap.Service(env.serviceUrl, env.accessToken)
// Open map
let res = await svc.openMap({
mapid: env.exampleMapId, // Map ID (ensure this ID exists; you can upload a new drawing to create one)
mapopenway: vjmap.MapOpenWay.GeomRender, // Open with geometry rendering
style: vjmap.openMapDarkStyle() // Use dark background style when div has dark background
})
if (res.error) {
console.error(res.error)
}
// Get map extent
let mapExtent = vjmap.GeoBounds.fromString(res.bounds);
// Create coordinate system
let prj = new vjmap.GeoProjection(mapExtent);
// Create map object
let map = new vjmap.Map({
container: 'map', // container ID
style: svc.rasterStyle(), // Raster tile style
center: prj.toLngLat(mapExtent.center()), // Center point
zoom: 2,
renderWorldCopies: false
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

# Map Load Complete

After creating the map object, map resources begin loading. The load event is triggered when map resources have finished loading.

map.on("load", function() {
// Triggered when map resources have finished loading
// Add custom data sources, custom layers
});
1
2
3
4

Or

await map.onLoad();
1

# Destroy Map Object

Call the remove method to destroy the map. After this method executes, the map object is unregistered, memory is released, and the map container is cleared.

// Destroy map and clear map container
map.remove();
1
2

# Set View

# Basic View Settings

setCenter([lng, lat])
Set map center.

// Using CAD coordinates
map.setCenter(map.toLngLat([1000, 2000]));
// Using longitude/latitude directly
map.setCenter([120.1551, 30.2741]);
1
2
3
4

setZoom(zoomLevel)
Set map zoom level.

map.setZoom(14);
1

setZoomAndCenter
Set map center and zoom level at the same time:

// Pass zoom level and center longitude/latitude together
map.setZoomAndCenter(12, [116.46, 39.92]);
1
2

setWheelZoomRate
Set map to integer-level zoom or non-integer zoom:

// Set map to integer-level zoom
map.scrollZoom.setWheelZoomRate(1); // Set zoom ratio per mouse wheel scroll
map.scrollZoom.setZoomRate(1); // Set touch screen zoom ratio, max value is 1
// Set map to non-integer zoom (default 1/450)
// map.scrollZoom.setWheelZoomRate(1 / 600); // Smaller rate value = finer map zoom
1
2
3
4
5

setBearing(angle)
Set map rotation angle.

map.setBearing(90);
1

setPitch(pitch)
Set map tilt angle.

map.setPitch(45);
1

jumpTo(options)
Jump instantly to specified map state (no animation).

map.jumpTo({ center: [20.1551, 30.2741], zoom: 14 });
1

flyTo(options)
Fly to new view state with animated transition.

map.flyTo({ center: [20.1551, 30.2741], zoom: 14 });
1

panBy([x, y], options)
Pan the map by pixels.

map.panBy([100, 0]);
1

fitBounds(bounds, options)
Automatically adjust map view to fit the bounding box.

// Based on CAD geometry coordinate extent
let bounds = vjmap.GeoBounds.fromString("100,200,300,400");
map.fitMapBounds(bounds);
1
2
3
// Based on longitude/latitude
map.fitBounds([[120.0, 30.0], [121.0, 31.0]]);
1
2

rotateTo(bearing, options)
Set map rotation to a specific angle.

map.rotateTo(90);
1

resetNorth(options)
Reset map rotation to true north.

map.resetNorth();
1

# View Limits

setMinZoom(minZoom)
Set the minimum allowed zoom level for the map.

map.setMinZoom(5);
1

setMaxZoom(maxZoom)
Set the maximum allowed zoom level for the map.

map.setMaxZoom(18);
1

setMaxBounds(bounds)
Limit the maximum bounds of the map; dragging beyond this range is disabled.

map.setMaxBounds([[120.0, 30.0], [121.0, 31.0]]);
1

# Map Events

on(eventType, listener)
Listen for map events such as click, move, zoom, etc.

map.on('click', (e) => {
console.log('Click position:', e.lngLat);
});
1
2
3

off(eventType, listener)
Remove listener for a specific event.

map.off('click', listenerFunction);
1

once(eventType, listener)
Listen for a specific event only once.

map.once('click', (e) => {
console.log('One-time click event:', e.lngLat);
});
1
2
3

onLoad(listener)
Listen for map load complete event.

map.on('load', () => {
console.log('Map loaded');
});
1
2
3

onMove(listener)
Listen for map pan event.

map.on('move', () => {
    console.log('Map is moving');
});
1
2
3

onZoom(listener)
Listen for map zoom event.

map.on('zoom', () => {
    console.log('Map is zooming');
});
1
2
3

# Add Controls

addControl(control, position)
Add controls (zoom, navigation, etc.).

const nav = new vjmap.NavigationControl();
map.addControl(nav, 'top-right');
1
2

removeControl(control)
Remove a control.

map.removeControl(nav);
1

addScaleControl(options)
Add scale control.

map.addControl(new vjmap.ScaleControl());
1

addFullScreenControl()
Add fullscreen control.

map.addControl(new vjmap.FullscreenControl());
1

# Add Layers

addLayer(layer)
Add a layer.

map.addLayer({
    id: 'circle-layer',
    type: 'circle',
    source: 'points',
    paint: {
        'circle-radius': 10,
        'circle-color': '#007cbf'
    }
});
1
2
3
4
5
6
7
8
9

addImage(name, image)
Add custom image for use in layers.

map.addImage('custom-icon', 'path-to-image.png');
1

addSource(sourceId, source)
Add custom data source.

map.addSource('points', {
    type: 'geojson',
    data: {
        type: 'FeatureCollection',
        features: []
    }
});
1
2
3
4
5
6
7

getLayer(layerId)
Get the specified layer object.

const layer = map.getLayer('custom-layer');
1

# Update and Remove Layers

setPaintProperty(layerId, property, value)
Dynamically set layer paint properties.

map.setPaintProperty('circle-layer', 'circle-radius', 15);
1

setLayoutProperty(layerId, property, value)
Dynamically set layer layout properties.

map.setLayoutProperty('circle-layer', 'visibility', 'none');
1

removeLayer(layerId)
Remove the specified layer.

map.removeLayer('custom-layer');
1

moveLayer(layerId, beforeId)
Change layer draw order by moving a layer before another layer.

map.moveLayer('circle-layer', 'another-layer');
1

# Dynamically Update Data Sources

getSource(sourceId)
Get the specified data source object.

const source = map.getSource('points');
1

setData(sourceId, data)
Update data in the specified data source.

map.getSource('points').setData(newGeoJsonData);
1

removeSource(sourceId)
Remove the specified data source.

map.removeSource('points');
1

removeSourceEx(sourceId)
Remove the specified data source and layers that depend on it.

map.removeSource('points');
1

# Map Style Management

setStyle(style)
Set map style.

map.setStyle(style);
1

getStyle()
Get current map style.

const style = map.getStyle();
1
  1. isStyleLoaded()
    Check if map style has finished loading.
if (map.isStyleLoaded()) {
    console.log('Style loaded');
}
1
2
3

# Get Map Information

getCenter()
Get current map center point.

const center = map.getCenter();
console.log(center); // {lng, lat}
1
2

getZoom()
Get current map zoom level.

const zoom = map.getZoom();
1

getBearing()
Get current map rotation angle.

const bearing = map.getBearing();
1

getPitch()
Get current map tilt angle.

const pitch = map.getPitch();
1

getBounds()
Get current map bounding box (coordinates of the four corners).

const bounds = map.getBounds();
1

getMaxZoom()
Get the maximum allowed zoom level for the map.

const maxZoom = map.getMaxZoom();
1

getMinZoom()
Get the minimum allowed zoom level for the map.

const minZoom = map.getMinZoom();
1

resize()
Resize the map; use when the map container size has changed.

map.resize();
1

toLngLat(point)
Convert CAD coordinates to longitude/latitude coordinates.

const lnglat = map.toLngLat([1000, 3000]);
1

fromLngLat(point)
Convert longitude/latitude coordinates to CAD coordinates.

const point = map.fromLngLat([120.1551, 30.2741]);
1

project(lngLat)
Convert longitude/latitude coordinates to pixel coordinates.

const point = map.project([120.1551, 30.2741]);
1

unproject(point)
Convert pixel coordinates to longitude/latitude coordinates.

const lngLat = map.unproject([200, 300]);
1

remove()
Destroy map object and release resources.

map.remove();
1

# Other Common Methods

getCanvas()
Get the map's canvas element.

const canvas = map.getCanvas();
1

Export map as base64 PNG image

let map = new vjmap.Map({
    container: 'map', // DIV container ID
    preserveDrawingBuffer: true, // If true, the map canvas can be exported to PNG using map.getCanvas().toDataURL(). This is false by default for performance optimization. (Optional, default false)
    ...
});
console.log(map.getCanvas().toDataURL())
1
2
3
4
5
6

getContainer()
Get the map's container element.

const container = map.getContainer();
1

queryRenderedFeatures([point], options)
Query all rendered features in the specified area.

const features = map.queryRenderedFeatures([200, 300], {
    layers: ['custom-layer']
});
1
2
3

getPadding()
Get current map padding.

const padding = map.getPadding();
1

setPadding(padding)
Set map padding.

map.setPadding({ top: 50, bottom: 50 });
1

getFreeCameraOptions()
Get map free camera options.

const cameraOptions = map.getFreeCameraOptions();
1

setFreeCameraOptions(options)
Set map free camera options.

map.setFreeCameraOptions(cameraOptions);
1

loadImage(url, callback)
Load image from URL and return in callback.

map.loadImage('path-to-image.png', (error, image) => {
    if (error) throw error;
    map.addImage('custom-icon', image);
});
1
2
3
4

removeImage(name)
Remove an added image.

map.removeImage('custom-icon');
1

listImages()
List all added image names.

const images = map.listImages();
console.log(images);
1
2

hasImage(name)
Check if an image with the specified name exists on the map.

const hasImage = map.hasImage('custom-icon');
1

addCustomLayer(layer)
Add custom WebGL layer.

map.addLayer({
    id: 'custom-webgl-layer',
    type: 'custom',
    renderingMode: '3d',
    onAdd: (map, gl) => {
        // Initialize WebGL resources
    },
    render: (gl, matrix) => {
        // Render WebGL layer
    }
});
1
2
3
4
5
6
7
8
9
10
11

hasLayer(layerId)
Check if a layer with the specified ID exists on the map.

const hasLayer = map.hasLayer('custom-layer');
1

getLayerVisibility(layerId)
Get layer visibility.

const visibility = map.getLayoutProperty('custom-layer', 'visibility');
1

getLight()
Get current light settings.

const light = map.getLight();
1

setLight(lightOptions)
Set light.

map.setLight({
    anchor: 'viewport',
    color: '#ffffff',
    intensity: 0.5
});
1
2
3
4
5

getFeatureState(featureId)
Get state of a specific feature.

const state = map.getFeatureState({ source: 'my-source', id: featureId });
1

setFeatureState(featureId, state)
Set state of a specific feature.

map.setFeatureState({ source: 'my-source', id: featureId }, { selected: true });
1

removeFeatureState(featureId)
Remove state of a specific feature.

map.removeFeatureState({ source: 'my-source', id: featureId });
1

getFilter(layerId)
Get layer filter condition.

const filter = map.getFilter('my-layer');
1

setFilter(layerId, filter)
Set layer filter condition.

map.setFilter('my-layer', ['==', 'property', 'value']);
1

hasClass(className)
Check if map container has the specified CSS class.

const hasClass = map.getContainer().classList.contains('my-class');
1

addClass(className)
Add CSS class to map container.

map.getContainer().classList.add('my-class');
1

removeClass(className)
Remove the specified CSS class from map container.

map.getContainer().classList.remove('my-class');
1

toggleClass(className)
Toggle the specified CSS class on map container.

map.getContainer().classList.toggle('my-class');
1

setCursor(cursorStyle)
Set map cursor style.

map.getCanvas().style.cursor = 'pointer';
1

resetCursor()
Reset map cursor style.

map.getCanvas().style.cursor = '';
1

isMoving()
Check if map is currently moving.

const moving = map.isMoving();
1

isZooming()
Check if map is currently zooming.

const zooming = map.isZooming();
1