@@ -46,6 +46,12 @@ class BuildsInfo extends HTMLElement {
4646 return ;
4747 }
4848
49+ this . dispatchEvent (
50+ new CustomEvent ( "data" , {
51+ detail : { data } ,
52+ } ) ,
53+ ) ;
54+
4955 const tileAnchor = ( name ) => {
5056 return `<a href="${ this . getUrl ( name ) } " download="${ name } ">${ name } </a>` ;
5157 } ;
@@ -135,7 +141,7 @@ class ExampleMap extends HTMLElement {
135141
136142 if ( ! this . innerHTML ) {
137143 this . innerHTML = `
138- <frame-layout ratio="16:9 ">
144+ <frame-layout ratio="4:3 ">
139145 <div id="example_map"></div>
140146 </frame-layout>
141147 <p class="mapAttribution">
@@ -151,11 +157,18 @@ class ExampleMap extends HTMLElement {
151157 this . map = new maplibre . Map ( {
152158 container : "example_map" ,
153159 style : this . mapStyle ,
154- center : [ - 1.615008 , 54.971191 ] ,
155- zoom : 13 ,
160+ center : [ - 4 , 56 ] ,
161+ zoom : 4 ,
156162 attributionControl : false ,
157163 } ) ;
158- this . map . once ( "styledata" , ( ) => this . render ( ) ) ;
164+ this . map . once ( "styledata" , ( ) => {
165+ this . render ( ) ;
166+ this . dispatchEvent (
167+ new CustomEvent ( "map" , {
168+ detail : { map : this . map } ,
169+ } ) ,
170+ ) ;
171+ } ) ;
159172 }
160173 }
161174
@@ -173,3 +186,88 @@ maplibre.addProtocol("pmtiles", protocol.tile);
173186
174187window . customElements . define ( "builds-info" , BuildsInfo ) ;
175188window . customElements . define ( "example-map" , ExampleMap ) ;
189+
190+ let map = null ;
191+ let data = null ;
192+
193+ document . querySelector ( "example-map" ) ?. addEventListener ( "map" , ( event ) => {
194+ map = event . detail . map ;
195+ updateMap ( ) ;
196+ } ) ;
197+
198+ document . querySelector ( "builds-info" ) ?. addEventListener ( "data" , ( event ) => {
199+ data = event . detail . data ;
200+ updateMap ( ) ;
201+ } ) ;
202+
203+ const colours = [ "#0371A6" , "#D7461A" ] ;
204+
205+ function updateMap ( ) {
206+ if ( ! data || ! map ) return ;
207+
208+ let source = map . getSource ( "targets" ) ;
209+ if ( ! source ) {
210+ map . addSource ( "targets" , {
211+ type : "geojson" ,
212+ data : { type : "FeatureCollection" , features : [ ] } ,
213+ } ) ;
214+ source = map . getSource ( "targets" ) ;
215+ }
216+
217+ const features = data . targets . map ( ( t , i ) => ( {
218+ type : "Feature" ,
219+ properties : {
220+ target_color : colours [ i % colours . length ] ,
221+ target_name : t . name ,
222+ } ,
223+ geometry : {
224+ type : "Polygon" ,
225+ coordinates : [
226+ [
227+ [ t . bbox [ 0 ] , t . bbox [ 1 ] ] ,
228+ [ t . bbox [ 0 ] , t . bbox [ 3 ] ] ,
229+ [ t . bbox [ 2 ] , t . bbox [ 3 ] ] ,
230+ [ t . bbox [ 2 ] , t . bbox [ 1 ] ] ,
231+ [ t . bbox [ 0 ] , t . bbox [ 1 ] ] ,
232+ ] ,
233+ ] ,
234+ } ,
235+ } ) ) ;
236+
237+ source . setData ( {
238+ type : "FeatureCollection" ,
239+ features,
240+ } ) ;
241+
242+ if ( ! map . getLayer ( "targets_outline" ) ) {
243+ map . addLayer ( {
244+ id : "targets_outline" ,
245+ type : "line" ,
246+ source : "targets" ,
247+ paint : {
248+ "line-width" : 2 ,
249+ "line-color" : [ "get" , "target_color" ] ,
250+ } ,
251+ } ) ;
252+ }
253+
254+ // NOTE: this doesn't work, but I'd like it to show the name in the bottom-right of the rectangle
255+ // if (!map.getLayer("targets_label")) {
256+ // map.addLayer({
257+ // id: "targets_label",
258+ // type: "symbol",
259+ // source: "targets",
260+ // paint: {
261+ // "text-color": ["get", "target_color"],
262+ // },
263+ // layout: {
264+ // "text-field": ["get", "target_name"],
265+ // "text-anchor": "bottom-right",
266+ // "text-font": ["Noto Sans Regular"],
267+ // "text-offset": [0, -1],
268+ // "text-size": 14,
269+ // // 'text-color': 'red'
270+ // },
271+ // });
272+ // }
273+ }
0 commit comments