For AI agents: a documentation index is available at /llms.txt. Markdown versions of all pages are available by appending .md to any URL.

Class ImageSourceMLGL

A data source containing an image. (See the Style Specification for detailed documentation of options.)

// add to map
map.addSource('some id', {
   type: 'image',
   url: 'https://www.maplibre.org/images/foo.png',
   coordinates: [
       [-76.54, 39.18],
       [-76.52, 39.18],
       [-76.52, 39.17],
       [-76.54, 39.17]
   ]
});

// update coordinates
let mySource = map.getSource('some id');
mySource.setCoordinates([
    [-76.54335737228394, 39.18579907229748],
    [-76.52803659439087, 39.1838364847587],
    [-76.5295386314392, 39.17683392507606],
    [-76.54520273208618, 39.17876344106642]
]);

// update url and coordinates simultaneously
mySource.updateImage({
   url: 'https://www.maplibre.org/images/bar.png',
   coordinates: [
       [-76.54335737228394, 39.18579907229748],
       [-76.52803659439087, 39.1838364847587],
       [-76.5295386314392, 39.17683392507606],
       [-76.54520273208618, 39.17876344106642]
   ]
})

// update with an already-decoded image (no network request)
const bitmap = await createImageBitmap(myCanvas);
mySource.updateImage({image: bitmap});

// create an empty source (no url), then feed it entirely via updateImage
map.addSource('empty id', {
   type: 'image',
   coordinates: [
       [-76.54, 39.18],
       [-76.52, 39.18],
       [-76.52, 39.17],
       [-76.54, 39.17]
   ]
});
(map.getSource('empty id') as ImageSource).updateImage({image: bitmap});

map.removeSource('some id');  // remove

Hierarchy (View Summary)

Implements

Index

Constructors

Properties

coordinates: Coordinates
dispatcher: Dispatcher
flippedWindingOrder: boolean
id: string

The id for the source. Must not be used by any existing source.

imageWarp: RasterImageWarp
map: MapMLGL
maxzoom: number

The maximum zoom level for the source.

minzoom: number

The minimum zoom level for the source.

options: any
terrainTileRanges: { [zoom: string]: CanonicalTileRange }

This object is used to store the range of terrain tiles that overlap with this tile. It is relevant for image tiles, as the image exceeds single tile boundaries.

texture: Texture | null
tileCoords: Point[]
tiles: { [_: string]: Tile }
tileSize: number

The tile size for the source.

type: string
url: string

Methods

  • Parameters

    • context: Context
    • projectionSubdividesTiles: boolean

    Returns Mesh | null

    The mesh the image is drawn with, or null to use the tile mesh of the current projection.

    The raster vertex shader evaluates imageWarp per vertex, so a pair of triangles only samples it at the four corners. That is enough for the affine mapping of a parallelogram, and for a purely projective mapping, whose straight lines survive linear interpolation across the diagonal. Anything in between is neither, and would be textured as two independently warped halves with a visible seam along that diagonal, so it needs the warp evaluated per grid cell.

    A projection that subdivides its own tile meshes already does this, so it keeps them.

  • True if the source has transition, false otherwise.

    Returns boolean

  • Returns a true if this instance of Evented or any forwardeed instances of Evented have a listener for the specified type.

    Parameters

    Returns boolean

    true if there is at least one registered listener for specified event type, false otherwise

  • Parameters

    Returns Promise<void>

  • True if the source is loaded, false otherwise.

    Returns boolean

  • This method does the heavy lifting of loading a tile. In most cases it will defer the work to the relevant worker source.

    Parameters

    • tile: Tile

      The tile to load

    Returns Promise<void>

  • Removes a previously registered event listener.

    Type Parameters

    Parameters

    • type: T

      The event type to remove listeners for.

    • listener: (event: SourceEventType[T]) => void

      The listener function to remove.

    Returns this

  • Adds a listener to a specified event type.

    Type Parameters

    Parameters

    • type: T

      The event type to add a listen for.

    • listener: (event: SourceEventType[T]) => void

      The function to be called when the event is fired. The listener function is called with the data object passed to fire, extended with target and type properties.

    Returns Subscription

  • This method is called when the source is added to the map.

    Parameters

    Returns void

  • Adds a listener that will be called only once to a specified event type.

    The listener will be called first time the event fires after the listener is registered.

    Type Parameters

    Parameters

    • type: T

      The event type to listen for.

    Returns Promise<SourceEventType[T]>

    a promise that resolves with the event

  • Adds a listener that will be called only once to a specified event type.

    The listener will be called first time the event fires after the listener is registered.

    Type Parameters

    Parameters

    • type: T

      The event type to listen for.

    • listener: (event: SourceEventType[T]) => void

      The function to be called when the event is fired the first time.

    Returns this

    this when a listener is provided

  • This method is called when the source is removed from the map.

    Returns void

  • Allows to execute a prepare step before the source is used.

    Returns void

  • Sets the image's coordinates and re-renders the map.

    Parameters

    • coordinates: Coordinates

      Four geographical coordinates, represented as arrays of longitude and latitude numbers, which define the corners of the image. The coordinates start at the top left corner of the image and proceed in clockwise order. They do not have to represent a rectangle.

    Returns this

  • Parameters

    Returns this

    Sets how the image is warped onto its coordinates and re-renders the map.

    This only has an effect while the coordinates do not form a rectangle, and it is not part of the style specification, so it does not survive map.setStyle.

    // Keep the image pinned to its corners while the user drags them around.
    map.getSource('some id').setWarp('flat');
    
  • Updates the image and, optionally, the coordinates. To avoid having the image flash after changing, set the raster-fade-duration paint property on the raster layer to 0.

    Provide exactly one of url (to fetch a new image over the network) or image (an already-decoded HTMLImageElement, HTMLCanvasElement, ImageBitmap or ImageData to display directly, without a network request).

    Parameters

    Returns this

Was this helpful?