mapbox-gl-js icon indicating copy to clipboard operation
mapbox-gl-js copied to clipboard

Allow image source without image/coordinates

Open stevage opened this issue 3 years ago • 2 comments

Motivation

I often use this pattern for GeoJSON sources:

  1. addSource('mysource', { type: 'geojson', data: { type:'FeatureCollection', features: [] }});
  2. addLayer({ id: 'mylayer', source: 'mysource' })
  3. Now fetch the actual data, manipulate it, and call setData() to set it.

I do this because:

  • I need to fetch the data asynchronously (but I don't want to hold up all my other init)
  • I want the layer to be in a predictable place in the style (which I wouldn't get if I waited for the data to load before creating the source and the layer).

This doesn't work with the Image type because you have to provide some kind of actual data.

It would be more flexible thus if the image type didn't require data, like the geojson type.

stevage avatar Jun 24 '21 03:06 stevage

Hmm, as a bit of a hacky workaround, you can do this:

{
  id:'mysource',
  type: 'image',
  url: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=',
  coordinates: [[0,1],[1,1],[1,0],[0,0],],
}

stevage avatar Jun 24 '21 03:06 stevage

Hello, I am interested in contributing to this issue.

I think that the best solution is to allow the image source to be nullable. In this state, it wouldn't be displayed on the map until updateImage is called. There are two ways to implement this that I can think of:

  1. Both the coordinates and url must be null or both not null.
  2. The coordinates and url can be independently null or not null but it's only displayed when both are not null. For example, the coordinates can be initially set with the url being null but the url is changed later.

I'm not sure which is best, or if there is another way that is preferable.

stampyzfanz avatar Oct 01 '22 10:10 stampyzfanz