-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add
styleimagemissing
event for on-demand images
The event gets fired when a layer needs an image that the map doesn't have. The developer has a chance to listen for this event and add an image within the callback in order to have it included. This event can be used to dynamically generate icons based on feature properties. fix #7587
- Loading branch information
Showing
6 changed files
with
133 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<div id='map'></div> | ||
|
||
<script> | ||
|
||
var map = new mapboxgl.Map({ | ||
container: 'map', | ||
style: 'mapbox://styles/mapbox/streets-v9' | ||
}); | ||
|
||
map.on('styleimagemissing', function(e) { | ||
var id = e.id; // id of the missing image | ||
|
||
// check if this missing icon is one this function can generate | ||
var prefix = 'square-rgb-'; | ||
if (id.indexOf(prefix) !== 0) return; | ||
|
||
// extract the color from the id | ||
var rgb = id.replace(prefix, '').split(',').map(Number); | ||
|
||
var width = 64; // The image will be 64 pixels square | ||
var bytesPerPixel = 4; // Each pixel is represented by 4 bytes: red, green, blue, and alpha. | ||
var data = new Uint8Array(width * width * bytesPerPixel); | ||
|
||
for (var x = 0; x < width; x++) { | ||
for (var y = 0; y < width; y++) { | ||
var offset = (y * width + x) * bytesPerPixel; | ||
data[offset + 0] = rgb[0]; // red | ||
data[offset + 1] = rgb[1]; // green | ||
data[offset + 2] = rgb[2]; // blue | ||
data[offset + 3] = 255; // alpha | ||
} | ||
} | ||
|
||
map.addImage(id, {width: width, height: width, data: data}); | ||
}); | ||
|
||
map.on('load', function () { | ||
map.addLayer({ | ||
"id": "points", | ||
"type": "symbol", | ||
"source": { | ||
"type": "geojson", | ||
"data": { | ||
"type": "FeatureCollection", | ||
"features": [{ | ||
"type": "Feature", | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [0, 0] | ||
}, | ||
"properties": { | ||
"color": "255,0,0" | ||
} | ||
}, { | ||
"type": "Feature", | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [50, 0] | ||
}, | ||
"properties": { | ||
"color": "255,209,28" | ||
} | ||
}, { | ||
"type": "Feature", | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [-50, 0] | ||
}, | ||
"properties": { | ||
"color": "242,127,32" | ||
} | ||
}] | ||
} | ||
}, | ||
"layout": { | ||
"icon-image": ["concat", "square-rgb-", ["get", "color"]] | ||
} | ||
}); | ||
}); | ||
|
||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/*--- | ||
title: Generate and add a missing icon to the map | ||
description: Dynamically generate a missing icon at runtime and add it to the map. | ||
tags: | ||
- styles | ||
- layers | ||
pathname: /mapbox-gl-js/example/add-image-missing-generated/ | ||
---*/ | ||
import Example from '../../components/example'; | ||
import html from './add-image-missing-generated.html'; | ||
export default Example(html); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters