Skip to content

Commit c637401

Browse files
committed
Migrate Marker to AdvancedMarkerElement
1 parent fa89e0a commit c637401

File tree

3 files changed

+70
-34
lines changed

3 files changed

+70
-34
lines changed

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@
1717
<script type="text/javascript" src="map.js" defer></script>
1818
<!-- Create your own key and white list your website: https://console.cloud.google.com/apis/credentials/ -->
1919
<!-- View usage: https://console.cloud.google.com/apis/dashboard -->
20-
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=weekly&key=AIzaSyB2VxJuGd7PQLeh6UDBR8Q8qC4ql_sYmI0&language=nl&region=nl&callback=initMap" defer></script>
20+
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=weekly&key=AIzaSyCKZTaMWxCzxnX9ssNSIWYZiAZsI3lJNO0&language=nl&region=nl&libraries=marker&loading=async&callback=initMap" defer></script>
2121
</body>
2222
</html>

map.css

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,18 @@ h2.info_window_heading {
5050
outline-style: solid;
5151
box-shadow: 0 0 0 4px #5A01A7;
5252
}
53+
54+
/* Municipality icons */
55+
.municipalityContainer {
56+
position: relative;
57+
text-align: center;
58+
color: black;
59+
font-size: 15px;
60+
}
61+
62+
.municipalityLabel {
63+
position: absolute;
64+
top: 50%;
65+
left: 50%;
66+
transform: translate(-50%, -50%);
67+
}

map.js

Lines changed: 54 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,32 @@ function initMap() {
742742
}
743743
}
744744

745+
/**
746+
* Create a marker icon.
747+
* @param {string} sourceUrl Link to marker image.
748+
* @param {boolean} isVisible Create a hidden marker, or not.
749+
* @param {string=} label Optional label.
750+
* @return {!HTMLDivElement} Marker node containing the image.
751+
*/
752+
function createMarkerIcon(sourceUrl, isVisible, label) {
753+
const iconContainer = document.createElement("div");
754+
if (!isVisible) {
755+
iconContainer.style.visibility = "hidden";
756+
}
757+
const icon = document.createElement("img");
758+
icon.src = sourceUrl;
759+
iconContainer.appendChild(icon);
760+
if (label === undefined) {
761+
return iconContainer;
762+
}
763+
iconContainer.classList.add("municipalityContainer");
764+
const labelContainer = document.createElement("div");
765+
labelContainer.classList.add("municipalityLabel");
766+
labelContainer.innerText = label;
767+
iconContainer.appendChild(labelContainer);
768+
return iconContainer;
769+
}
770+
745771
/**
746772
* Add a marker to the map.
747773
* @param {!Object} publication Publication object.
@@ -781,10 +807,7 @@ function initMap() {
781807
function onMouseOver() {
782808
appState.markersArray.forEach(function (markerObject) {
783809
if (markerObject.url === publication.urlDoc) {
784-
markerObject.marker.setIcon({
785-
"url": "img/" + iconName + "-highlight.png",
786-
"size": iconSize
787-
});
810+
markerObject.marker.content.getElementsByTagName("img")[0].src = "img/" + iconName + "-highlight.png";
788811
}
789812
});
790813
}
@@ -795,7 +818,7 @@ function initMap() {
795818
function onMouseOut() {
796819
appState.markersArray.forEach(function (markerObject) {
797820
if (markerObject.url === publication.urlDoc) {
798-
markerObject.marker.setIcon(icon);
821+
markerObject.marker.content.getElementsByTagName("img")[0].src = "img/" + iconName + ".png";
799822
}
800823
});
801824
}
@@ -806,21 +829,13 @@ function initMap() {
806829
// "TVM- 7 PV reserveren - Monnikendammerweg 27-37 - 03-07/10/2022, Monnikendammerweg 27";
807830
// 125171;
808831
// 488983
809-
// https://developers.google.com/maps/documentation/javascript/reference#MarkerOptions
832+
// https://developers.google.com/maps/documentation/javascript/reference/advanced-markers#AdvancedMarkerElementOptions
810833
const age = getDaysPassed(publication.date);
811834
const iconName = getIconName(publication.title, publication.type);
812-
const iconSize = new google.maps.Size(35, 45); // Make sure image is already scaled
813-
const icon = {
814-
"url": "img/" + iconName + ".png",
815-
"size": iconSize
816-
};
817-
const marker = new google.maps.Marker({
835+
const marker = new google.maps.marker.AdvancedMarkerElement({
818836
"map": appState.map,
819837
"position": position,
820-
"clickable": true,
821-
"optimized": true,
822-
"visible": isMarkerVisible(age, periodToShow),
823-
"icon": icon,
838+
"content": createMarkerIcon("img/" + iconName + ".png", isMarkerVisible(age, periodToShow)),
824839
"zIndex": appState.zIndex,
825840
"title": publication.title
826841
});
@@ -835,8 +850,8 @@ function initMap() {
835850
appState.markersArray.push(markerObject);
836851
marker.addListener("click", onClick);
837852
// Highlight the icon (and related icons) on hover
838-
marker.addListener("mouseover", onMouseOver);
839-
marker.addListener("mouseout", onMouseOut);
853+
marker.content.addEventListener("mouseover", onMouseOver);
854+
marker.content.addEventListener("mouseout", onMouseOut);
840855
return markerObject;
841856
}
842857

@@ -959,18 +974,10 @@ function initMap() {
959974
const municipalityNames = Object.keys(appState.municipalities);
960975
municipalityNames.forEach(function (municipalityName) {
961976
const municipalityObject = appState.municipalities[municipalityName];
962-
let marker = new google.maps.Marker({
977+
const marker = new google.maps.marker.AdvancedMarkerElement({
963978
"map": appState.map,
964979
"position": municipalityObject.center,
965-
"label": municipalityName, // https://developers.google.com/maps/documentation/javascript/reference/marker#MarkerLabel
966-
"clickable": true,
967-
"optimized": true,
968-
//"scaleControl": true,
969-
"visible": municipalityName !== appState.activeMunicipality,
970-
"icon": {
971-
"url": "img/gemeente.png",
972-
"size": new google.maps.Size(50, 61) // Make sure image is already scaled
973-
},
980+
"content": createMarkerIcon("img/gemeente.png", municipalityName !== appState.activeMunicipality, municipalityName),
974981
"title": municipalityName
975982
});
976983
appState.municipalityMarkers.push({
@@ -991,6 +998,20 @@ function initMap() {
991998
});
992999
}
9931000

1001+
/**
1002+
* Show or hide a marker.
1003+
* @param {!Object} marker Marker object.
1004+
* @param {boolean} isVisible Set visibility.
1005+
* @return {void}
1006+
*/
1007+
function setMarkerVisibility(marker, isVisible) {
1008+
if (isVisible) {
1009+
marker.content.style.visibility = "visible";
1010+
} else {
1011+
marker.content.style.visibility = "hidden";
1012+
}
1013+
}
1014+
9941015
/**
9951016
* Reset time filter. This is done when the municipality is changed.
9961017
* @return {void}
@@ -1015,7 +1036,7 @@ function initMap() {
10151036
}
10161037
}
10171038
appState.markersArray.forEach(function (markerObject) {
1018-
markerObject.marker.setVisible(isMarkerVisible(markerObject.age, periodFilter.periodToShow));
1039+
setMarkerVisibility(markerObject.marker, isMarkerVisible(markerObject.age, periodFilter.periodToShow));
10191040
});
10201041
}
10211042
}
@@ -1231,11 +1252,11 @@ function initMap() {
12311252
{
12321253
"backgroundColor": "#9CC0F9", // https://developers.google.com/maps/documentation/javascript/reference/map#MapOptions.backgroundColor
12331254
"clickableIcons": false, // https://developers.google.com/maps/documentation/javascript/reference/map#MapOptions.clickableIcons
1234-
// Paid feature - "mapId": "c2a918307d540be7",
12351255
"center": new google.maps.LatLng(mapSettings.center.lat, mapSettings.center.lng),
12361256
"mapTypeId": google.maps.MapTypeId.ROADMAP, // https://developers.google.com/maps/documentation/javascript/reference/map#MapTypeId
12371257
"gestureHandling": "greedy", // When scrolling, keep scrolling
1238-
"zoom": mapSettings.zoomLevel
1258+
"zoom": mapSettings.zoomLevel,
1259+
"mapId": "fa8c70ada3bf211a"
12391260
}
12401261
);
12411262
determineRequestPeriod();
@@ -1387,7 +1408,7 @@ function initMap() {
13871408
function hideActiveMunicipalityMarker() {
13881409
appState.municipalityMarkers.forEach(function (markerObject) {
13891410
if (markerObject.municipalityName === appState.activeMunicipality) {
1390-
markerObject.marker.setVisible(false);
1411+
setMarkerVisibility(markerObject.marker, false);
13911412
}
13921413
});
13931414
}
@@ -1551,7 +1572,7 @@ function initMap() {
15511572
markerObject.marker.setMap(null);
15521573
});
15531574
appState.municipalityMarkers.forEach(function (markerObject) {
1554-
markerObject.marker.setVisible(markerObject.municipalityName !== municipalityToHide);
1575+
setMarkerVisibility(markerObject.marker, markerObject.municipalityName !== municipalityToHide);
15551576
});
15561577
appState.markersArray = [];
15571578
appState.delayedMarkersArray = [];

0 commit comments

Comments
 (0)