mapbox-gl-js
mapbox-gl-js copied to clipboard
Allow image source without image/coordinates
Motivation
I often use this pattern for GeoJSON sources:
- addSource('mysource', { type: 'geojson', data: { type:'FeatureCollection', features: [] }});
- addLayer({ id: 'mylayer', source: 'mysource' })
- 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.
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],],
}
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:
- Both the coordinates and url must be null or both not null.
- 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.