@@ -5,6 +5,7 @@ var initial = [ -60.643775, -32.954626]
55var zoom = 1.5
66var url = 'https://raw.githubusercontent.com/ELC/jupyter-map/master/jupyter-map.geojson' ;
77var places ; // Get value async below
8+ var filterInput = document . getElementById ( 'filter-input' ) ;
89
910var map = new mapboxgl . Map ( {
1011 container : 'map' ,
@@ -43,8 +44,56 @@ async function addData() {
4344 }
4445 } ) ;
4546
47+ // Filter by Input
48+
49+ let layerIDs = [ ] ;
50+
51+ places . features . forEach ( function ( feature ) {
52+ var symbol = feature . properties [ 'institution_name' ] ;
53+ var layerID = symbol . trim ( ) . toLowerCase ( ) ;
54+
55+ // Add a layer for this symbol type if it hasn't been added already.
56+ if ( ! map . getLayer ( layerID ) ) {
57+ map . addLayer ( {
58+ "id" : layerID ,
59+ "type" : "symbol" ,
60+ "source" : "data" ,
61+ "layout" : {
62+ "icon-image" : "marker-15" ,
63+ "icon-allow-overlap" : true ,
64+ "icon-size" : 1.5 ,
65+ "text-field" : "" ,
66+ 'text-allow-overlap' : true ,
67+ 'text-size' : 14 ,
68+ "text-letter-spacing" : 0.05 ,
69+ "text-offset" : [ 0 , 2 ] ,
70+ } ,
71+ "paint" : {
72+ "text-color" : "#202" ,
73+ "text-halo-color" : "#fff" ,
74+ "text-halo-width" : 2
75+ } ,
76+ "filter" : [ "==" , "institution_name" , symbol ]
77+ } ) ;
78+
79+ layerIDs . push ( layerID ) ;
80+ }
4681 } ) ;
4782
83+ filterInput . addEventListener ( 'keyup' , function ( e ) {
84+ // If the input value matches a layerID set
85+ // it's visibility to 'visible' or else hide it.
86+ map . setLayoutProperty ( 'places' , "visibility" ,
87+ ! e . target . value ? "visible" : "none" ) ;
88+
89+ var value = e . target . value . trim ( ) . toLowerCase ( ) ;
90+ layerIDs . forEach ( function ( layerID ) {
91+ map . setLayoutProperty ( layerID , 'visibility' ,
92+ layerID . includes ( value ) ? 'visible' : 'none' ) ;
93+ map . setLayoutProperty ( layerID , 'text-field' ,
94+ ( e . target . value && layerID . includes ( value ) ) ? '{institution_name}' : '' ) ;
95+ } ) ;
96+ } ) ;
4897}
4998
5099// First time
@@ -153,6 +202,9 @@ map.on('mouseleave', 'places', function(e) {
153202} ) ;
154203
155204map . on ( 'click' , function ( e ) {
205+ filterInput . value = ""
206+ let event = new Event ( 'keyup' ) ;
207+ filterInput . dispatchEvent ( event )
156208 map . getCanvas ( ) . style . cursor = '' ;
157209} ) ;
158210
0 commit comments