SDK Reference
Class: ShareMap
Class ShareMap
The ShareMap class is the core component of the sharemap-maplib-js library. It represents a map on the page and provides a comprehensive API to control the map's view, style, layers, and interactivity. Behind the scenes, it utilizes WebGL to render high-performance, interactive maps from vector tiles.

Initialization
Initialize or use the ShareMap via the snippet below.
javascript
// Initialize a new high-performance map instance
import { Map, NavigationControl } from 'sharemap-maplib-js';
const map = new Map({
container: 'map-container',
style: 'https://api.sharemap.live/maps/basic/style.json',
center: [106.660172, 10.762622],
zoom: 12,
pitch: 45,
bearing: -17.6
});
// Add zoom and rotation controls to the map.
map.addControl(new NavigationControl(), 'top-right');Key Instance Methods
addControl(control: IControl, position?: string): thisAdds an IControl instance (like NavigationControl, ScaleControl, etc.) to the map at the specified position.
addLayer(layer: AnyLayer, beforeId?: string): thisAdds a new Mapbox style layer to the map. If beforeId is provided, the layer is inserted before the specified layer.
addSource(id: string, source: AnySourceData): thisAdds a new data source to the map. Layers can then reference this source to render data.
on(type: string, listener: Function): thisSubscribes a listener function to events emitted by the map (e.g., click, zoom, moveend, render).
flyTo(options: AnimationOptions, eventData?: Object): thisAnimates the map view to a new center, zoom, pitch, and bearing with a smooth flight animation.
setCenter(center: LngLatLike, eventData?: Object): thisInstantly sets the map's geographical centerpoint without animation.
setZoom(zoom: number, eventData?: Object): thisInstantly sets the map's zoom level.
Parameters
container
string | HTMLElementThe HTML element, or its string ID, where the map will be rendered. If this is omitted, the map will not be attached to the DOM automatically.
style
string | ObjectThe map style URL or a JSON object conforming to the Mapbox Style Specification. ShareMap provides several default styles via api.sharemap.live.
center
LngLatLikeThe initial geographical centerpoint of the map in [lng, lat] format.
zoom
numberThe initial zoom level of the map, ranging from 0 (entire world) to 22 (street level).
pitch
numberThe initial pitch (tilt) of the map, measured in degrees away from the plane of the screen (0-85).
bearing
numberThe initial bearing (rotation) of the map, measured in degrees counter-clockwise from north.
Live Demo
Performance Tip
For high-traffic applications, ensure you call map.remove() when unmounting components to prevent memory leaks in Single Page Applications (SPAs).