Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<!--
 @license
 Copyright 2019 Google LLC. All Rights Reserved.
 SPDX-License-Identifier: Apache-2.0
-->
<!-- [START maps_advanced_markers_simple] -->
<html>
  <head>
    <title>Default Advanced Marker</title>
    <script src="https://polyfill.io/v3/polyfill.min.js?features=default"></script>
    <link rel="stylesheet" type="text/css" href="./style.css" />
    <script type="module" src="./index.js"></script>
  </head>
  <body>
    <div id="map"></div>
    <!-- prettier-ignore -->
    <script>(g=>{var h,a,k,p="The Google Maps JavaScript API",c="google",l="importLibrary",q="__ib__",m=document,b=window;b=b[c]||(b[c]={});var d=b.maps||(b.maps={}),r=new Set,e=new URLSearchParams,u=()=>h||(h=new Promise(async(f,n)=>{await (a=m.createElement("script"));e.set("libraries",[...r]+"");for(k in g)e.set(k.replace(/[A-Z]/g,t=>"_"+t[0].toLowerCase()),g[k]);e.set("callback",c+".maps."+q);a.src=`https://maps.${c}apis.com/maps/api/js?`+e;d[q]=f;a.onerror=()=>h=n(Error(p+" could not load."));a.nonce=m.querySelector("script[nonce]")?.nonce||"";m.head.append(a)}));d[l]?console.warn(p+" only loads once. Ignoring:",g):d[l]=(f,...n)=>r.add(f)&&u().then(()=>d[l](f,...n))})
        ({key: "YOUR_API_KEY", v: "beta"});</script>
  </body>
</html>
<!-- [END maps_advanced_markers_simple] -->
 
let map;
async function initMap() {
    
    // instantiate the libraries
    const {Marker} = await google.maps.importLibrary("marker");
    const { event } = await google.maps.importLibrary("core");
  const { Map } = await google.maps.importLibrary("maps");
    
    // instantiate the map
  map = new Map(document.getElementById("map"), {
    center: { lat: -34.397, lng: 150.644 },
    zoom: 8,
  });
    
    // instantiate the marker
    marker = new Marker({
        map,
        draggable: true,
        position: {lat: -34.397, lng: 150.644},
    })
    
    // This adds a click event listener to your marker 
    let markerListenerEvent = event.addListener(marker, "click", function() {
        alert("you have clicked the marker!")
    });
    
    // this will show in the console that 
    // the marker now have a click event listener.
    console.log("clickable marker: ");
    console.log(marker.__e3_.click[64].So);
    // this will return "true"
    
    
  // clicking on the map will then remove the listener you put
    // on the marker
    let mapListenerEvent = event.addListener(map, "click", function() {
        alert("You have now removed the marker event listener!")
        // This is the the method that removes the listener for marker above
        event.removeListener(markerListenerEvent);
        
        // this will show in the console that 
        // the marker now don't have a click event listener
        // since the "click" object will now be empty.
        console.log("not clickable marker: ");
        console.log(marker.__e3_.click);
        // this will return an empty object
    })
}
initMap();
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
Yrll21pro
0viewers