@@ -15,6 +15,7 @@ let map: google.maps.Map;
15
15
let countryMenu : HTMLSelectElement ;
16
16
let featureMenu : HTMLSelectElement ;
17
17
let searchInput : HTMLInputElement ;
18
+ let searchInputOption : HTMLInputElement ;
18
19
let fillColorPicker : HTMLInputElement ;
19
20
let strokeColorPicker : HTMLInputElement ;
20
21
let contentDiv : HTMLElement ;
@@ -45,6 +46,7 @@ function initMap() {
45
46
const card = document . getElementById ( 'pac-card' ) as HTMLElement ;
46
47
contentDiv = document . getElementById ( 'pac-content' ) as HTMLElement ;
47
48
searchInput = document . getElementById ( 'pac-input' ) as HTMLInputElement ;
49
+ searchInputOption = document . getElementById ( 'pac-filter-option' ) as HTMLInputElement ;
48
50
countryMenu = document . getElementById ( 'country-select' ) as HTMLSelectElement ;
49
51
featureMenu = document . getElementById ( 'feature-type-select' ) as HTMLSelectElement ;
50
52
fillColorPicker = document . getElementById ( 'fill-color-picker' ) as HTMLInputElement ;
@@ -70,6 +72,18 @@ function initMap() {
70
72
autoComplete = new google . maps . places . Autocomplete ( searchInput , autocompleteOptions ) ;
71
73
placesService = new google . maps . places . PlacesService ( map ) ;
72
74
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
+
73
87
autoComplete . addListener ( 'place_changed' , ( ) => {
74
88
const place = autoComplete . getPlace ( ) as google . maps . places . PlaceResult ;
75
89
const types = place . types as string [ ] ;
@@ -98,10 +112,11 @@ function initMap() {
98
112
break ;
99
113
}
100
114
101
- showSelectedPolygon ( place . place_id ) ;
115
+ showSelectedPolygon ( place . place_id , 1 ) ;
102
116
103
117
} ) ;
104
118
119
+
105
120
// Add all the feature layers.
106
121
//@ts -ignore
107
122
countryLayer = map . getFeatureLayer ( 'COUNTRY' ) ;
@@ -133,7 +148,7 @@ function initMap() {
133
148
// Set up country and feature menus.
134
149
buildMenu ( ) ;
135
150
onCountrySelected ( ) ;
136
- featureMenu . selectedIndex = 1 ;
151
+ featureMenu . selectedIndex = 0 ; // Set to COUNTRY.
137
152
}
138
153
139
154
// Restyle and make a request when the feature type is changed.
@@ -148,34 +163,52 @@ function featureTypeChanged() {
148
163
} ) ;
149
164
150
165
revertStyles ( ) ;
166
+ setFeatureType ( ) ;
151
167
selectedPlaceId = '' ;
152
168
contentDiv . innerHTML = '' ;
153
169
154
170
// Apply the style to the selected feature layer.
155
171
switch ( featureMenu . value ) {
156
172
case 'country' :
157
173
countryLayer . style = styleStrokeOnly ;
174
+ searchInputOption . disabled = false ;
158
175
break ;
159
176
case 'administrative_area_level_1' :
160
177
admin1Layer . style = styleStrokeOnly ;
178
+ searchInputOption . disabled = false ;
161
179
break ;
162
180
case 'administrative_area_level_2' :
163
181
admin2Layer . style = styleStrokeOnly ;
182
+ searchInputOption . disabled = false ;
164
183
break ;
165
184
case 'locality' :
166
185
localityLayer . style = styleStrokeOnly ;
186
+ searchInputOption . disabled = false ;
167
187
break ;
168
188
case 'postal_code' :
169
189
postalCodeLayer . style = styleStrokeOnly ;
190
+ searchInputOption . disabled = false ;
170
191
break ;
171
192
case 'school_district' :
172
193
schoolDistrictLayer . style = styleStrokeOnly ;
194
+ searchInputOption . disabled = true ;
173
195
break ;
174
196
default :
175
197
break ;
176
198
}
177
199
}
178
200
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
+
179
212
// Restyle when the stroke or fill is changed.
180
213
function styleChanged ( ) {
181
214
if ( selectedPlaceId ) {
@@ -216,21 +249,27 @@ function applyStyle(placeid?) {
216
249
switch ( featureMenu . value ) {
217
250
case 'country' :
218
251
countryLayer . style = featureStyle ;
252
+ searchInputOption . disabled = false ;
219
253
break ;
220
254
case 'administrative_area_level_1' :
221
255
admin1Layer . style = featureStyle ;
256
+ searchInputOption . disabled = false ;
222
257
break ;
223
258
case 'administrative_area_level_2' :
224
259
admin2Layer . style = featureStyle ;
260
+ searchInputOption . disabled = false ;
225
261
break ;
226
262
case 'locality' :
227
263
localityLayer . style = featureStyle ;
264
+ searchInputOption . disabled = false ;
228
265
break ;
229
266
case 'postal_code' :
230
267
postalCodeLayer . style = featureStyle ;
268
+ searchInputOption . disabled = false ;
231
269
break ;
232
270
case 'school_district' :
233
271
schoolDistrictLayer . style = featureStyle ;
272
+ searchInputOption . disabled = true ;
234
273
break ;
235
274
default :
236
275
break ;
@@ -255,10 +294,28 @@ function buildMenu() {
255
294
function onCountrySelected ( ) {
256
295
// Get the selected value.
257
296
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
+ }
258
310
311
+ showSelectedCountry ( countryMenu . options [ countryMenu . selectedIndex ] . text ) ;
312
+ }
313
+
314
+ // Return a map of feature availability for a country.
315
+ function getFeatureAvailability ( countryName ) {
259
316
// Return the data for the selected country.
260
317
const selectedCountry = countries . find ( ( country ) => {
261
- return country . code === selected ;
318
+ return country . code === countryName ;
262
319
} ) ;
263
320
264
321
// Create a map for our values.
@@ -271,19 +328,7 @@ function onCountrySelected() {
271
328
[ 'school_district' , selectedCountry ?. feature . school_district ] ,
272
329
] ) ;
273
330
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 ;
287
332
}
288
333
289
334
// Restyle all feature layers to be invisible.
@@ -297,7 +342,7 @@ function revertStyles() {
297
342
function handlePlaceClick ( event ) {
298
343
let clickedPlaceId = event . features [ 0 ] . placeId ;
299
344
if ( ! clickedPlaceId ) return ;
300
- showSelectedPolygon ( clickedPlaceId ) ;
345
+ showSelectedPolygon ( clickedPlaceId , 0 ) ;
301
346
}
302
347
303
348
// Get the place ID for the selected country, then call showSelectedPolygon().
@@ -315,13 +360,14 @@ function showSelectedCountry(countryName) {
315
360
status === google . maps . places . PlacesServiceStatus . OK &&
316
361
place
317
362
) {
318
- showSelectedPolygon ( place [ 0 ] . place_id ) ;
363
+ showSelectedPolygon ( place [ 0 ] . place_id , 0 ) ;
319
364
}
320
365
} ) ;
321
366
}
322
367
323
368
// Event handler for when a polygon is selected.
324
- function showSelectedPolygon ( placeid ) {
369
+ // mode 0 = click, mode 1 = autocomplete.
370
+ function showSelectedPolygon ( placeid , mode ) {
325
371
selectedPlaceId = placeid ;
326
372
contentDiv . innerHTML = '' ; // Clear the info display.
327
373
@@ -353,17 +399,23 @@ function showSelectedPolygon(placeid) {
353
399
const types = place . types as string [ ] ;
354
400
355
401
// 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
+ } ;
359
411
}
360
412
} ) ;
361
413
362
414
// Call the global styling function.
363
415
applyStyle ( placeid ) ;
364
-
365
416
}
366
- /** GENERATED FILE, DO NOT EDIT */
417
+
418
+ /** GENERATED CONTENT, DO NOT EDIT BELOW THIS LINE */
367
419
368
420
const countries = [
369
421
{
0 commit comments