googlemaps / js-samples

Samples for the Google Maps JavaScript v3 API
https://developers.google.com/maps/documentation/javascript/
Other
744 stars 821 forks source link

Bug: event-click-latlng #1230

Open rodrigojp2005 opened 2 years ago

rodrigojp2005 commented 2 years ago

Operating system

ubuntu 22.04

Browser Version

Chrome 104.0.5112.101 (oficial) 64 bits

How severe is the bug?

moderate

Bug description

function initMap() { const myLatlng = { lat: -25.363, lng: 131.044 };

const map = new google.maps.Map(document.getElementById("map")!, { zoom: 4, center: myLatlng, });

// Create the initial InfoWindow. let infoWindow = new google.maps.InfoWindow({ content: "Click the map to get Lat/Lng!", position: myLatlng, });

infoWindow.open(map);

// Configure the click listener. map.addListener("click", (mapsMouseEvent) => { // Close the current InfoWindow. infoWindow.close();

// Create a new InfoWindow.
infoWindow = new google.maps.InfoWindow({
  position: mapsMouseEvent.latLng,
});
infoWindow.setContent(
  JSON.stringify(mapsMouseEvent.latLng.toJSON(), null, 2)
);
infoWindow.open(map);

}); }

declare global { interface Window { initMap: () => void; } } window.initMap = initMap; export {};

Steps to reproduce

Just run.

Console log output

I fixed it by commenting some parts below
 <script>
  function initMap() {
    const myLatlng = { lat: -25.363, lng: 131.044 };

  const map = new google.maps.Map(document.getElementById('map'), {
    zoom: 4,
    center: myLatlng,
  });

  // Create the initial InfoWindow.
  let infoWindow = new google.maps.InfoWindow({
    content: "Click the map to get Lat/Lng!",
    position: myLatlng,
  });

  infoWindow.open(map);

  // Configure the click listener.
  map.addListener("click", (mapsMouseEvent) => {
    // Close the current InfoWindow.
    infoWindow.close();

    // Create a new InfoWindow.
    infoWindow = new google.maps.InfoWindow({
      position: mapsMouseEvent.latLng,
    });
    infoWindow.setContent(
      JSON.stringify(mapsMouseEvent.latLng.toJSON(), null, 2)
    );
    infoWindow.open(map);
  });
}
/*
declare global {
  interface Window {
    initMap: () => void;
  }
}*/
window.initMap = initMap;
//export {};
    </script>
wangela commented 2 years ago

Hi, please describe the issue. What is the expected behavior and what happens instead? If you are seeing an error message in the JavaScript console, share that as well.