"Hello World" Comparison:
Marker (Legacy)
google.maps.Marker is deprecated (February 21st, 2024 (v3.56)).
Docs: https://developers.google.com/maps/documentation/javascript/markers
// Initialize and add the map
let map;
function initMap() {
const myLatLng = { lat: -25.363, lng: 131.044 };
const map = new google.maps.Map(document.getElementById("map"), {
zoom: 4,
disableDefaultUI: true, // a way to quickly hide all controls
});
new google.maps.Marker({
position: myLatLng,
map,
});
}
initMap();
AdvancedMarkerElement (new)
DOCS: https://developers.google.com/maps/documentation/javascript/advanced-markers/overview
Extra step for AdvancedMarkerElement
is to Create a map ID.
// Initialize and add the map
let map;
async function initMap_AdvancedMarkerElement() {
const myLatLng = { lat: -25.363, lng: 131.044 };
const { Map } = await google.maps.importLibrary("maps");
const { AdvancedMarkerElement } = await google.maps.importLibrary("marker");
// The map, centered at Uluru
map = new Map(document.getElementById("AdvancedMarker_MAP"), {
zoom: 4,
center: myLatLng,
disableDefaultUI: true, // a way to quickly hide all controls
mapId: "DEMO_MAP_ID",
});
// The marker, positioned at Uluru
const marker = new AdvancedMarkerElement({
map: map,
position: myLatLng,
title: "Uluru",
});
}
initMap_AdvancedMarkerElement();
Related: asynchronous JavaScript
IMPORTANT - A lot of code ideas (Like changing marker icon, marker styles and so on) - completely changed VS Legacy (Follow the docs).
PRE steps (Legacy & Advanced):
In both cases you should Get an API key and enable the Maps JavaScript API.
https://developers.google.com/maps/documentation/javascript/get-api-key
And of course to load the API script (with your api key)
<script async
src="https://maps.googleapis.com/maps/api/js?your-key=&loading=async&callback=initMap">
</script>
More ways to Load the Maps JavaScript API:
https://developers.google.com/maps/documentation/javascript/load-maps-js-api
- CSS: Add Height to map div. For example:
#map {height: 400px;}
google.maps.Marker
is not scheduled to be discontinued and At least 12 months notice will be given before support is discontinued so where's the problem? And if there's anything you don't know how to do with advanced markers then ask an on-topic question. – Hatchery