@@ -15,6 +15,7 @@ let map: google.maps.Map;
1515let countryMenu : HTMLSelectElement ;
1616let featureMenu : HTMLSelectElement ;
1717let searchInput : HTMLInputElement ;
18+ let searchInputOption : HTMLInputElement ;
1819let fillColorPicker : HTMLInputElement ;
1920let strokeColorPicker : HTMLInputElement ;
2021let contentDiv : HTMLElement ;
@@ -45,6 +46,7 @@ function initMap() {
4546 const card = document . getElementById ( 'pac-card' ) as HTMLElement ;
4647 contentDiv = document . getElementById ( 'pac-content' ) as HTMLElement ;
4748 searchInput = document . getElementById ( 'pac-input' ) as HTMLInputElement ;
49+ searchInputOption = document . getElementById ( 'pac-filter-option' ) as HTMLInputElement ;
4850 countryMenu = document . getElementById ( 'country-select' ) as HTMLSelectElement ;
4951 featureMenu = document . getElementById ( 'feature-type-select' ) as HTMLSelectElement ;
5052 fillColorPicker = document . getElementById ( 'fill-color-picker' ) as HTMLInputElement ;
@@ -70,6 +72,18 @@ function initMap() {
7072 autoComplete = new google . maps . places . Autocomplete ( searchInput , autocompleteOptions ) ;
7173 placesService = new google . maps . places . PlacesService ( map ) ;
7274
75+ searchInputOption . addEventListener ( 'change' , ( ) => {
76+ if ( searchInputOption . checked ) {
77+ // Do not show school_district since AC cannot use it for filtering.
78+ featureMenu . item ( 5 ) ! . disabled = true ;
79+ setFeatureType ( ) ;
80+ } else {
81+ // Show school districts.
82+ featureMenu . item ( 5 ) ! . disabled = false ;
83+ setFeatureType ( ) ;
84+ }
85+ } ) ;
86+
7387 autoComplete . addListener ( 'place_changed' , ( ) => {
7488 const place = autoComplete . getPlace ( ) as google . maps . places . PlaceResult ;
7589 const types = place . types as string [ ] ;
@@ -98,10 +112,11 @@ function initMap() {
98112 break ;
99113 }
100114
101- showSelectedPolygon ( place . place_id ) ;
115+ showSelectedPolygon ( place . place_id , 1 ) ;
102116
103117 } ) ;
104118
119+
105120 // Add all the feature layers.
106121 //@ts -ignore
107122 countryLayer = map . getFeatureLayer ( 'COUNTRY' ) ;
@@ -133,7 +148,7 @@ function initMap() {
133148 // Set up country and feature menus.
134149 buildMenu ( ) ;
135150 onCountrySelected ( ) ;
136- featureMenu . selectedIndex = 1 ;
151+ featureMenu . selectedIndex = 0 ; // Set to COUNTRY.
137152}
138153
139154// Restyle and make a request when the feature type is changed.
@@ -148,34 +163,52 @@ function featureTypeChanged() {
148163 } ) ;
149164
150165 revertStyles ( ) ;
166+ setFeatureType ( ) ;
151167 selectedPlaceId = '' ;
152168 contentDiv . innerHTML = '' ;
153169
154170 // Apply the style to the selected feature layer.
155171 switch ( featureMenu . value ) {
156172 case 'country' :
157173 countryLayer . style = styleStrokeOnly ;
174+ searchInputOption . disabled = false ;
158175 break ;
159176 case 'administrative_area_level_1' :
160177 admin1Layer . style = styleStrokeOnly ;
178+ searchInputOption . disabled = false ;
161179 break ;
162180 case 'administrative_area_level_2' :
163181 admin2Layer . style = styleStrokeOnly ;
182+ searchInputOption . disabled = false ;
164183 break ;
165184 case 'locality' :
166185 localityLayer . style = styleStrokeOnly ;
186+ searchInputOption . disabled = false ;
167187 break ;
168188 case 'postal_code' :
169189 postalCodeLayer . style = styleStrokeOnly ;
190+ searchInputOption . disabled = false ;
170191 break ;
171192 case 'school_district' :
172193 schoolDistrictLayer . style = styleStrokeOnly ;
194+ searchInputOption . disabled = true ;
173195 break ;
174196 default :
175197 break ;
176198 }
177199}
178200
201+ // Toggle autocomplete types based on restrict search checkbox.
202+ function setFeatureType ( ) {
203+ if ( searchInputOption . checked ) {
204+ // Set autocomplete to the selected type.
205+ autoComplete . setTypes ( [ featureMenu . value ] ) ;
206+ } else {
207+ // Set autocomplete to return all feature types.
208+ autoComplete . setTypes ( [ '(regions)' ] ) ;
209+ }
210+ }
211+
179212// Restyle when the stroke or fill is changed.
180213function styleChanged ( ) {
181214 if ( selectedPlaceId ) {
@@ -216,21 +249,27 @@ function applyStyle(placeid?) {
216249 switch ( featureMenu . value ) {
217250 case 'country' :
218251 countryLayer . style = featureStyle ;
252+ searchInputOption . disabled = false ;
219253 break ;
220254 case 'administrative_area_level_1' :
221255 admin1Layer . style = featureStyle ;
256+ searchInputOption . disabled = false ;
222257 break ;
223258 case 'administrative_area_level_2' :
224259 admin2Layer . style = featureStyle ;
260+ searchInputOption . disabled = false ;
225261 break ;
226262 case 'locality' :
227263 localityLayer . style = featureStyle ;
264+ searchInputOption . disabled = false ;
228265 break ;
229266 case 'postal_code' :
230267 postalCodeLayer . style = featureStyle ;
268+ searchInputOption . disabled = false ;
231269 break ;
232270 case 'school_district' :
233271 schoolDistrictLayer . style = featureStyle ;
272+ searchInputOption . disabled = true ;
234273 break ;
235274 default :
236275 break ;
@@ -255,10 +294,28 @@ function buildMenu() {
255294function onCountrySelected ( ) {
256295 // Get the selected value.
257296 let selected = countryMenu . value ;
297+ let featureAvailabilityMap = getFeatureAvailability ( selected ) ;
298+
299+ // Set the feature list selection to 'country'.
300+ featureMenu . namedItem ( 'country' ) ! . selected = true ;
301+
302+ // Do a comparison on the map, and disable any false items.
303+ for ( const item of featureAvailabilityMap ) {
304+ if ( item [ 1 ] == false ) {
305+ featureMenu . namedItem ( item [ 0 ] ) ! . disabled = true ;
306+ } else {
307+ featureMenu . namedItem ( item [ 0 ] ) ! . disabled = false ;
308+ }
309+ }
258310
311+ showSelectedCountry ( countryMenu . options [ countryMenu . selectedIndex ] . text ) ;
312+ }
313+
314+ // Return a map of feature availability for a country.
315+ function getFeatureAvailability ( countryName ) {
259316 // Return the data for the selected country.
260317 const selectedCountry = countries . find ( ( country ) => {
261- return country . code === selected ;
318+ return country . code === countryName ;
262319 } ) ;
263320
264321 // Create a map for our values.
@@ -271,19 +328,7 @@ function onCountrySelected() {
271328 [ 'school_district' , selectedCountry ?. feature . school_district ] ,
272329 ] ) ;
273330
274- // Set the feature list selection to 'country'.
275- featureMenu . namedItem ( 'country' ) ! . selected = true ;
276-
277- // Do a comparison on the map, and disable any false items.
278- for ( const item of featureAvailabilityMap ) {
279- if ( item [ 1 ] == false ) {
280- featureMenu . namedItem ( item [ 0 ] ) ! . disabled = true ;
281- } else {
282- featureMenu . namedItem ( item [ 0 ] ) ! . disabled = false ;
283- }
284- }
285-
286- showSelectedCountry ( countryMenu . options [ countryMenu . selectedIndex ] . text ) ;
331+ return featureAvailabilityMap ;
287332}
288333
289334// Restyle all feature layers to be invisible.
@@ -297,7 +342,7 @@ function revertStyles() {
297342function handlePlaceClick ( event ) {
298343 let clickedPlaceId = event . features [ 0 ] . placeId ;
299344 if ( ! clickedPlaceId ) return ;
300- showSelectedPolygon ( clickedPlaceId ) ;
345+ showSelectedPolygon ( clickedPlaceId , 0 ) ;
301346}
302347
303348// Get the place ID for the selected country, then call showSelectedPolygon().
@@ -315,13 +360,14 @@ function showSelectedCountry(countryName) {
315360 status === google . maps . places . PlacesServiceStatus . OK &&
316361 place
317362 ) {
318- showSelectedPolygon ( place [ 0 ] . place_id ) ;
363+ showSelectedPolygon ( place [ 0 ] . place_id , 0 ) ;
319364 }
320365 } ) ;
321366}
322367
323368// Event handler for when a polygon is selected.
324- function showSelectedPolygon ( placeid ) {
369+ // mode 0 = click, mode 1 = autocomplete.
370+ function showSelectedPolygon ( placeid , mode ) {
325371 selectedPlaceId = placeid ;
326372 contentDiv . innerHTML = '' ; // Clear the info display.
327373
@@ -353,17 +399,23 @@ function showSelectedPolygon(placeid) {
353399 const types = place . types as string [ ] ;
354400
355401 // Create HTML for place information.
356- contentDiv . innerHTML = '<hr><span id="place-info"><b>' + place . formatted_address +
357- '</b><br/> Place ID: <code>' + placeid + '</code>' +
358- '<br/> Feature type: <code>' + types [ 0 ] + '</code></span><br/>' ;
402+ // Show information if boundary was clicked (mode 0).
403+ if ( mode == 0 ) {
404+ contentDiv . innerHTML = `<hr><span id="place-info"><b>${ place . formatted_address }
405+ </b><br/> Place ID: <code>${ placeid } </code>
406+ <br/> Feature type: <code>${ types [ 0 ] } </code></span><br/>` ;
407+ } else {
408+ // Display no information if autocomplete was used (mode 1).
409+ contentDiv . innerHTML = `<hr><span id="place-info">Click a boundary to see its place ID and feature type.</span>`
410+ } ;
359411 }
360412 } ) ;
361413
362414 // Call the global styling function.
363415 applyStyle ( placeid ) ;
364-
365416}
366- /** GENERATED FILE, DO NOT EDIT */
417+
418+ /** GENERATED CONTENT, DO NOT EDIT BELOW THIS LINE */
367419
368420const countries = [
369421 {
0 commit comments