Skip to content

Commit 908dbd4

Browse files
authored
feat: click event for any layer types (#9)
feat: click event for any layer types
1 parent 6510e9d commit 908dbd4

File tree

5 files changed

+149
-291
lines changed

5 files changed

+149
-291
lines changed

demo/app/main-page.ts

-8
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,6 @@ export function onMapReady(args) {
108108
])
109109
.then(() => {
110110
console.log('main-page Markers added');
111-
setTimeout(() => {
112-
map.queryRenderedFeatures({
113-
point: {
114-
lat: 52.360216,
115-
lng: 4.889168,
116-
},
117-
}).then((result) => console.log(JSON.stringify(result)));
118-
}, 1000);
119111
})
120112
.catch((error) => {
121113
console.error('main-page: error adding markers:', error);

demo/app/main-view-model.ts

+66-14
Original file line numberDiff line numberDiff line change
@@ -426,12 +426,31 @@ export class HelloWorldModel extends Observable {
426426
source: {
427427
type: 'geojson',
428428
data: {
429-
type: 'Feature',
430-
properties: {},
431-
geometry: {
432-
type: 'Point',
433-
coordinates: [4.823684692382513, 52.3701494345567],
434-
},
429+
type: 'FeatureCollection',
430+
features: [
431+
{
432+
id: '1',
433+
type: 'Feature',
434+
properties: {
435+
querySample: '1',
436+
},
437+
geometry: {
438+
type: 'Point',
439+
coordinates: [4.823684692382513, 52.3701494345567],
440+
},
441+
},
442+
{
443+
id: '2',
444+
type: 'Feature',
445+
properties: {
446+
querySample: '2',
447+
},
448+
geometry: {
449+
type: 'Point',
450+
coordinates: [4.823684692382513, 52.3701494345567],
451+
},
452+
},
453+
],
435454
},
436455
},
437456
paint: {
@@ -444,7 +463,25 @@ export class HelloWorldModel extends Observable {
444463
'circle-stroke-opacity': 0.75,
445464
},
446465
})
447-
.then(() => console.log('circle-with-source-object added'));
466+
.then(() => {
467+
console.log('circle-with-source-object added');
468+
this.mapbox.onMapEvent('click', 'circle-with-source-object', (features) => {
469+
console.log('clicked', 'circle-with-source-object', features);
470+
});
471+
472+
setTimeout(() => {
473+
this.mapbox
474+
.queryRenderedFeatures({
475+
point: {
476+
lat: 52.3701494345567,
477+
lng: 4.823684692382513
478+
},
479+
layers: ['circle-with-source-object'],
480+
filter: ['all', ['==', '$id', '2']],
481+
})
482+
.then((result) => console.log('query rendered features', JSON.stringify(result)));
483+
}, 3000);
484+
});
448485

449486
this.mapbox
450487
.addLayer({
@@ -476,7 +513,12 @@ export class HelloWorldModel extends Observable {
476513
'line-dash-array': [1, 1, 1, 1],
477514
},
478515
})
479-
.then(() => console.log('line-with-source-object added'));
516+
.then(() => {
517+
console.log('line-with-source-object added');
518+
this.mapbox.onMapEvent('click', 'line-with-source-object', (features) => {
519+
console.log('clicked', 'line-with-source-object', features);
520+
});
521+
});
480522

481523
this.mapbox
482524
.addLayer({
@@ -510,7 +552,12 @@ export class HelloWorldModel extends Observable {
510552
'fill-translate-anchor': 'map',
511553
},
512554
})
513-
.then(() => console.log('fill-with-source-object added'));
555+
.then(() => {
556+
console.log('fill-with-source-object added');
557+
this.mapbox.onMapEvent('click', 'fill-with-source-object', (features) => {
558+
console.log('clicked', 'fill-with-source-object', features);
559+
});
560+
});
514561

515562
await this.mapbox.addImage('bee', 'res://bee');
516563
await this.mapbox.addImage('pizza', '~/assets/pizza-slice.png');
@@ -532,7 +579,12 @@ export class HelloWorldModel extends Observable {
532579
'text-color': '#d6c80d',
533580
},
534581
})
535-
.then(() => console.log('symbol-with-source-object added'));
582+
.then(() => {
583+
console.log('symbol-with-source-object added');
584+
this.mapbox.onMapEvent('click', 'symbol-with-source-object', (features) => {
585+
console.log('clicked', 'symbol-with-source-object', features);
586+
});
587+
});
536588

537589
this.mapbox
538590
.addLayer({
@@ -567,10 +619,10 @@ export class HelloWorldModel extends Observable {
567619

568620
public doRemoveLayerAndSource(): void {
569621
Promise.all([
570-
this.mapbox.removeLayer('circle-with-source-object'),
571-
this.mapbox.removeLayer('line-with-source-object'),
572-
this.mapbox.removeLayer('fill-with-source-object'),
573-
this.mapbox.removeLayer('symbol-with-source-object'),
622+
this.mapbox.removeLayer('circle-with-source-object').then(() => this.mapbox.offMapEvent('click', 'circle-with-source-object')),
623+
this.mapbox.removeLayer('line-with-source-object').then(() => this.mapbox.offMapEvent('click', 'line-with-source-object')),
624+
this.mapbox.removeLayer('fill-with-source-object').then(() => this.mapbox.offMapEvent('click', 'fill-with-source-object')),
625+
this.mapbox.removeLayer('symbol-with-source-object').then(() => this.mapbox.offMapEvent('click', 'symbol-with-source-object')),
574626
this.mapbox.removeLayer('symbol-with-source-object2'),
575627
]).then(() => {
576628
return Promise.all([

0 commit comments

Comments
 (0)