Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
Mountain View desk5 min

Adding Markers to a Google Map with JavaScript and jQuery (Advanced Markers)

A current, practical guide to adding multiple Google Maps AdvancedMarkerElement instances, with optional jQuery iteration, map-ID setup, cleanup, and legacy-marker migration advice.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use Google Maps Platform’s google.maps.marker.AdvancedMarkerElement for new marker code. Load the Maps JavaScript API and its marker library, create a map with a map ID, then create one marker for each location. jQuery is optional: it can wait for the page, select elements, or iterate your location data, but Google’s API creates and manages the map and marker objects.

What you need before writing the code

  • A Google Cloud project with billing enabled.
  • A Maps JavaScript API key (or an OAuth token) and the Maps JavaScript API enabled for that project.
  • A map ID. Google’s DEMO_MAP_ID is suitable for testing; create and use a map ID owned by your project in production.
  • A map container with an explicit height. Without a height, the map can exist in the DOM but remain invisible.

Maps Platform charges depend on the request type and SKU. Do not assume that every map load is free or rely on an old flat-price figure; check Google’s current billing documentation for your account and region.

Programmatic Advanced Markers: the current approach

This complete example keeps the marker work in ordinary JavaScript and uses jQuery only to run code after the document is ready and to iterate the application data.

HTML and CSS

<div id="map"></div>

<style>
  #map {
    height: 420px;
    width: 100%;
  }
</style>

JavaScript with a jQuery loop

<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script>
  const locations = [
    { title: "New York", position: { lat: 40.7128, lng: -74.0060 } },
    { title: "Chicago", position: { lat: 41.8781, lng: -87.6298 } },
    { title: "Los Angeles", position: { lat: 34.0522, lng: -118.2437 } }
  ];

  async function initMap() {
    const { Map } = await google.maps.importLibrary("maps");
    const { AdvancedMarkerElement } =
      await google.maps.importLibrary("marker");

    const map = new Map(document.getElementById("map"), {
      center: locations[0].position,
      zoom: 4,
      mapId: "DEMO_MAP_ID" // Replace with your project map ID in production.
    });

    const markers = [];

    $.each(locations, function (_index, location) {
      const marker = new AdvancedMarkerElement({
        map,
        position: location.position,
        title: location.title
      });
      markers.push(marker);
    });

    // Keep `markers` if the application will update or remove markers later.
    window.mapMarkers = markers;
  }

  $(function () {
    initMap().catch(function (error) {
      console.error("Google Maps failed to initialize", error);
    });
  });
</script>

The API calls are deliberately explicit: importLibrary("maps") supplies the map class and importLibrary("marker") supplies AdvancedMarkerElement. The jQuery $.each call only walks your data; it does not create a Google marker.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Loading the Maps JavaScript API

Load the Maps JavaScript API using Google’s current loader configuration and your key, then call initMap. A typical script-tag configuration is:

<script async
  src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&loading=async&callback=initMap">
</script>

If you use the callback form, define initMap on the global scope before the loader invokes it and do not also initialize the map from a separate jQuery-ready callback. Choose one startup path. The earlier example uses jQuery readiness; in that case, load the API without a callback and call initMap yourself after the API loader is available.

Updating and removing markers

Keep references to marker instances when locations can change. To remove an advanced marker, set its map property to null; setting its position to null is also supported.

function removeMarker(marker) {
  marker.map = null;
}

function clearMarkers(markers) {
  markers.forEach(function (marker) {
    marker.map = null;
  });
  markers.length = 0;
}

To move an existing marker, assign a new position rather than creating a second marker:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
marker.position = { lat: 39.7392, lng: -104.9903 };

AdvancedMarkerElement versus the web-component style

Google documents two current ways to use advanced markers. They produce the same kind of map marker but fit different application architectures.

Approach How markers are created Best fit Lifecycle considerations
Programmatic API new AdvancedMarkerElement({ map, position, title }) Applications that fetch, filter, or update locations in JavaScript Store marker references so updates and removal are deterministic
Web components <gmp-map> containing <gmp-advanced-marker> elements Pages already organized around custom elements and declarative markup Let the component/DOM lifecycle own creation and teardown; verify integration details for your framework

Do not mix both styles for the same marker set unless you have a specific reason. In a component-based application, make sure markers are not duplicated when a view re-renders.

Declarative markup outline

<gmp-map center="40.7128,-74.0060" zoom="4" map-id="DEMO_MAP_ID">
  <gmp-advanced-marker
    position="40.7128,-74.0060"
    title="New York">
  </gmp-advanced-marker>
</gmp-map>

This style requires the Maps web-component library and its corresponding loader configuration. Use a project map ID rather than the demo ID when deploying.

Legacy google.maps.Marker code

Older tutorials commonly use new google.maps.Marker(...). Google deprecated google.maps.Marker on February 21, 2024, with Maps JavaScript API v3.56. Google says the class is not currently scheduled for discontinuation, but recommends AdvancedMarkerElement for new implementations. Existing code may continue to work, yet new projects should not start on the deprecated class.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Legacy marker Advanced marker
new google.maps.Marker({ map, position }) new AdvancedMarkerElement({ map, position })
Does not require the advanced marker library or a map ID in the same way Requires the marker library and a map ID
Deprecated as of API v3.56 (February 21, 2024) Google’s recommended choice for new marker implementations

Common failures and fixes

The map area is blank

  • Give the map container a non-zero CSS height.
  • Check the browser console for an invalid key, disabled API, billing, or referrer-restriction error.
  • Confirm that the map is initialized only after the Maps loader has finished.

“AdvancedMarkerElement is undefined”

The marker library was not imported. Await google.maps.importLibrary("marker") and use the returned AdvancedMarkerElement class.

The marker is rejected or does not render

Advanced markers require a map ID. Use DEMO_MAP_ID while testing or a map ID from your own Cloud project in production.

Markers appear twice

Your startup code is running from both a loader callback and a jQuery-ready handler, or a component is rendering the same data repeatedly. Initialize once and clear or reuse the stored marker references before rebuilding the set.

Nothing happens after changing the data array

Changing your application data does not automatically change existing map objects. Iterate the new data, update existing marker positions where possible, create markers for new records, and set removed markers’ map properties to null.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Choosing jQuery or plain JavaScript

Use jQuery if the rest of the page already depends on it or if its iteration and event helpers simplify your application code. Use plain JavaScript when you do not otherwise need the dependency. Neither choice changes the Google Maps API requirements: the map ID, marker-library import, credentials, and marker lifecycle remain the same.

Frequently Asked Questions

Can I still use the old google.maps.Marker class?

Yes, Google says it is not currently scheduled for discontinuation, but it has been deprecated since February 21, 2024, and AdvancedMarkerElement is recommended for new code.

Is jQuery required for Google Maps markers?

No. jQuery can handle page readiness or data iteration, while the Maps JavaScript API creates the map and marker instances.

Why does an AdvancedMarkerElement need a map ID?

Google requires a map ID for advanced markers. DEMO_MAP_ID is available for testing; production deployments should use a map ID from your own project.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. Shenzhen desk3 min
    HONOR Expands Beyond Smartphones With Humanoid Robot RevealHONOR said it unveiled its first humanoid robot at MWC 2026 and named shopping assistance, workplace inspections, and supportive companionship as intended uses. Later Robotics D1 claims and a reported…
  2. Cupertino desk5 min
    Apple Unveils AirPods Max 2: The Upgrade That Should Have Happened Years AgoAirPods Max 2 adds H2-powered audio features and Apple claims up to 1.5× more effective ANC, but its design, Smart Case, and 20-hour battery rating are unchanged. Wired lossless audio…
  3. Cupertino desk4 min
    Apple’s OLED Touch MacBooks Are Coming—but the Dynamic Island Is the Real GambleApple has not announced an OLED touchscreen MacBook, but reports point to high-end models arriving in late 2026 or early 2027. The reported Mac Dynamic Island could be useful, but…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.