1/* global google */ 2/** 3 * Initializes Google Maps for all elements with the class 'cu-google-map'. 4 * Checks for API availability, parses coordinates, and adds markers. 5 * @global 6 */ 7window.initMap = function () { 8 // Check if Google Maps API has loaded 9 if ( 10 typeof google === 'undefined' || 11 ! google.maps || 12 ! google.maps.marker || 13 ! google.maps.marker.AdvancedMarkerElement 14 ) { 15 return; 16 } 17 18 /** 19 * Array of all map elements. 20 * @type {Element[]} 21 */ 22 const mapElements = document.getElementsByClassName( 'cu-google-map' ); 23 // Check if there are any map elements. If not, return early 24 if ( ! mapElements.length ) { 25 return; 26 } 27 28 Array.from( mapElements ).forEach( ( mapElement ) => { 29 /** 30 * ID of the map element. 31 * @type {string} 32 */ 33 const mapId = mapElement.id; 34 /** 35 * Raw coordinates from data attribute. 36 * @type {Object} 37 */ 38 const rawCoordinates = JSON.parse( 39 mapElement.dataset.coordinates || '{}' 40 ); 41 /** 42 * Coordinates for the map marker. 43 * @type {{lat: number, lng: number}} 44 */ 45 const coordinates = { 46 lat: parseFloat( rawCoordinates.lat ), 47 lng: parseFloat( rawCoordinates.lng ), 48 }; 49 50 const zoom = parseInt( mapElement.dataset.zoom ) || 15; 51 52 if ( isNaN( coordinates.lat ) || isNaN( coordinates.lng ) ) { 53 // Fallback to default coordinates if parsing fails 54 coordinates.lat = 45.3850225; 55 coordinates.lng = -75.6946679; 56 } 57 58 /** 59 * Address for the marker title. 60 * @type {string} 61 */ 62 const address = 63 mapElement.dataset.address || 64 'Carleton University, 1125 Colonel By Dr, Ottawa, ON K1S 5B6'; 65 66 // Initialize the map 67 const map = new google.maps.Map( mapElement, { 68 zoom, 69 center: coordinates, 70 mapId, 71 } ); 72 73 // Add a marker using AdvancedMarkerElement 74 new google.maps.marker.AdvancedMarkerElement( { 75 position: coordinates, 76 map, 77 title: address, 78 } ); 79 } ); 80};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.