|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2026 Google LLC. All Rights Reserved. |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +// [START maps_dds_datasets_point] |
| 8 | +const mapElement = document.querySelector('gmp-map') as google.maps.MapElement; |
| 9 | +let innerMap; |
| 10 | +// [START maps_dds_datasets_point_style_function] |
| 11 | +function setStyle(/* FeatureStyleFunctionOptions */ params) { |
| 12 | + // [START maps_dds_datasets_point_style_get_features] |
| 13 | + // Get the dataset feature, so we can work with all of its attributes. |
| 14 | + const datasetFeature = params.feature; |
| 15 | + // Get all of the needed dataset attributes. |
| 16 | + const furColors = datasetFeature.datasetAttributes['CombinationofPrimaryandHighlightColor']; |
| 17 | + // [END maps_dds_datasets_point_style_get_features] |
| 18 | + |
| 19 | + // Apply styles. Fill is primary fur color, stroke is secondary fur color. |
| 20 | + switch (furColors) { |
| 21 | + case 'Black+': |
| 22 | + return /* FeatureStyleOptions */ { fillColor: 'black', pointRadius: 8 }; |
| 23 | + break; |
| 24 | + case 'Cinnamon+': |
| 25 | + return /* FeatureStyleOptions */ { fillColor: '#8b0000', pointRadius: 8 }; |
| 26 | + break; |
| 27 | + case 'Cinnamon+Gray': |
| 28 | + return /* FeatureStyleOptions */ { fillColor: '#8b0000', strokeColor: 'gray', pointRadius: 6 }; |
| 29 | + break; |
| 30 | + case 'Cinnamon+White': |
| 31 | + return /* FeatureStyleOptions */ { fillColor: '#8b0000', strokeColor: 'white', pointRadius: 6 }; |
| 32 | + break; |
| 33 | + case 'Gray+': |
| 34 | + return /* FeatureStyleOptions */ { fillColor: 'gray', pointRadius: 8 }; |
| 35 | + break; |
| 36 | + case 'Gray+Cinnamon': |
| 37 | + return /* FeatureStyleOptions */ { fillColor: 'gray', strokeColor: '#8b0000', pointRadius: 6 }; |
| 38 | + break; |
| 39 | + case 'Gray+Cinnamon, White': |
| 40 | + return /* FeatureStyleOptions */ { fillColor: 'silver', strokeColor: '#8b0000', pointRadius: 6 }; |
| 41 | + break; |
| 42 | + case 'Gray+White': |
| 43 | + return /* FeatureStyleOptions */ { fillColor: 'gray', strokeColor: 'white', pointRadius: 6 }; |
| 44 | + break; |
| 45 | + default: // Color not defined. |
| 46 | + return /* FeatureStyleOptions */ { fillColor: 'yellow', pointRadius: 8 }; |
| 47 | + break; |
| 48 | + } |
| 49 | +} |
| 50 | +// [END maps_dds_datasets_point_style_function] |
| 51 | + |
| 52 | +async function initMap() { |
| 53 | + // Request needed libraries. |
| 54 | + await google.maps.importLibrary('maps') as google.maps.MapsLibrary; |
| 55 | + |
| 56 | + // Get the inner map. |
| 57 | + innerMap = mapElement.innerMap; |
| 58 | + |
| 59 | + await google.maps.event.addListenerOnce(innerMap, 'idle', () => { |
| 60 | + // Add the data legend. |
| 61 | + makeLegend(innerMap); |
| 62 | + }); |
| 63 | + |
| 64 | + // Dataset ID for squirrel dataset. |
| 65 | + const datasetId = 'a99635b0-5e73-4b2a-8ae3-cb40f4b7f47e'; |
| 66 | + const datasetLayer = innerMap.getDatasetFeatureLayer(datasetId); |
| 67 | + datasetLayer.style = setStyle; |
| 68 | +} |
| 69 | + |
| 70 | +// Creates a legend for the map. |
| 71 | +async function makeLegend(innerMap) { |
| 72 | + let colors = { |
| 73 | + 'black': ['black'], |
| 74 | + 'cinnamon': ['#8b0000'], |
| 75 | + 'cinnamon + gray': ['#8b0000','gray'], |
| 76 | + 'cinnamon + white': ['#8b0000', 'white'], |
| 77 | + 'gray': ['gray'], |
| 78 | + 'gray + cinnamon': ['gray', '#8b0000'], |
| 79 | + 'gray + cinnamon + white': ['silver', '#8b0000'], |
| 80 | + 'gray + white': ['gray', 'white'], |
| 81 | + 'no color data': ['yellow'], |
| 82 | + }; |
| 83 | + |
| 84 | + let legend = document.getElementById('legend'); |
| 85 | + legend!.id = 'legend'; |
| 86 | + let title = document.createElement('div'); |
| 87 | + title.innerText = 'Fur Colors'; |
| 88 | + title.classList.add('title'); |
| 89 | + legend!.appendChild(title); |
| 90 | + let color; |
| 91 | + for (color in colors) { |
| 92 | + let wrapper = document.createElement('div'); |
| 93 | + wrapper.id = 'container'; |
| 94 | + let box = document.createElement('div'); |
| 95 | + box.style.backgroundColor = colors[color][0]; |
| 96 | + if (colors[color][1]) { |
| 97 | + box.style.borderColor = colors[color][1]; |
| 98 | + } else { |
| 99 | + box.style.borderColor = colors[color][0]; |
| 100 | + } |
| 101 | + box.classList.add('box'); |
| 102 | + let txt = document.createElement('div'); |
| 103 | + txt.classList.add('legend'); |
| 104 | + txt.innerText = color; |
| 105 | + wrapper.appendChild(box); |
| 106 | + wrapper.appendChild(txt); |
| 107 | + legend!.appendChild(wrapper); |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +initMap(); |
| 112 | +// [END maps_dds_datasets_point] |
0 commit comments